Book Image

PhoneGap for Enterprise

By : Kerri Shotts
Book Image

PhoneGap for Enterprise

By: Kerri Shotts

Overview of this book

Table of Contents (16 chapters)
PhoneGap for Enterprise
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Communicating with the backend using XHR


Most of the time, you'll want to communicate with your backend server by using XMLHttpRequest to send and receive JSON-formatted data. From now on, we'll use XHR as an abbreviation for this term.

XHR was designed by Microsoft in order to allow web applications to communicate with other servers and increase web app dynamism and responsiveness. XHR was later implemented by other browser vendors, and it eventually became a W3C standard.

The first version of XHR was amazing for the time, but it became apparent that additional flexibility was needed, and so XHR level 2 (XHR2) was created. Built on the original standard, XHR2 increased security by providing support for cross-domain requests using Cross-Origin Resource Sharing (CORS). It also enhanced XHR's flexibility by allowing many more data types to be sent and received.

A very simple XHR request looks like the following:

var xhr = new XMLHttpRequest();
xhr.open ( "GET", "http://www.example.com/api/getPeople...