Chapter 6. Exchanging Data: AJAX

In this chapter, we will cover:

  • How to send HTTP requests
  • Processing JSON responses
  • Sending cross-domain requests

Introduction

Modern web applications make intensive use of AJAX. This is a concept that can be defined as a technique of using the JavaScript programming language for sending the HTTP request to a remote server without the need to reload the page. The main advantage of this technique is the speed, no doubt. We don't need to reload all the content of the page causing many extra requests. Other times, reloading a page implies that we carry out only one request but if this requests returns a lot of information - for example, 50 records with 10 fields for each one, we need more time. Apart from the speed, another clear advantage of AJAX is the savings in bandwidth consumption. If we make few requests, we're saving bandwidth. In fact, the user can perceive this savings bandwidth as a faster response.

AJAX is a set of technologies such as XML, XHTML, and the DOM model where JavaScript acts as glue between them. Thanks to JavaScript, developers can invoke server-side code from the client side avoiding unneeded additional requests.

One of the first problems for web developers is how to deal with AJAX when users work with different web browsers. Inspite of many efforts for using a standard way, the current web browsers employ different JavaScript objects for handling AJAX requests and responses. Within this scenario it could be very useful to use a framework for encapsulating this behavior. For iPhone applications we can use different frameworks, which offer us components for working with AJAX in an easier manner. Specifically, we're going to work with three of them: Sencha Touch, XUI, and WebApp.Net.

In this chapter, you'll learn the core of AJAX: how to send different HTTP requests and how to process responses. Pertaining to these responses, you find out how to deal with different and common data formats such as JSON (JavaScript Object Notation) and XML. One of the recipes of this chapter handles an important aspect of AJAX. We're talking about how to do cross-domain requests.

The server side plays a fundamental role in the AJAX technology because we need a component to respond to the request. In practice, we can use any of the current server-side technologies in our iPhone applications. This means that we'll need to get a set of backend or server-side components ready to interact with the client side. Currently, some of the most famous and extended of these technologies are PHP, Ruby on Rails, Django, and ASP.NET. A lot of providers offer hosting services based on one or more of these technologies. Probably, the production machine for serving your iPhone web applications has already installed the required software for applying the mentioned technologies.

The three recipes covered, simply attempt to show you the fundamentals of using AJAX in your iPhone web applications. The presented concepts are only the foundation of building complex and interactive applications.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.139.8