Posting data to REST service in PhoneGap/Cordova

In the Consuming REST service from PhoneGap/Cordova entry i talked about how you can consume a REST service in PhoneGap/Cordova application using JQuery. In that application i was making a HTTP GET request to get data from REST service, i wanted to figure out how to make a HTTP POST call to REST service to create a new contact. You can download the index.html for my application from here This is screen shot of how my form looks like
This is screen shot of success message that i get if the contact insertion is successful

I followed these steps to build the CordovaManageContact application
  1. I followed the instructions in Getting Started with Android to build application that points to index.html inside the application, i tried it once to make sure that it works
  2. Then i changed the index.html file for my application to look like this
    <!DOCTYPE html>
    <title>Manage Contact</title>
    <script type="text/javascript" 
    charset="utf-8" src="cordova-1.7.0.js"></script>
    <script src=""></script>
    <script type="text/javascript" charset="utf-8">
      function insertContact(){
        console.log("Entering insertContact()");
        $("#insertContact :input").serializeArray(), 
          if(json== null || json == 'undefined')
            alert("Insert failed");
            alert("Insert successful");
        return false;
      <h3>Insert Contact</h3>
      <form id="insertContact">
            <td>Contact Id</td>
            <td><input type="text" name="contactId" /></td>
            <td>First Name</td>
            <td><input type="text" name="firstName" /></td>
            <td>Last Name</td>
            <td><input type="text" name="lastName" /></td>
            <td><input type="text" name="email" /></td>
            <td><input type="submit" 
      id="submit" name="submit" value="Submit" /></td>
    When the user clicks on submit button control goes to insertContact() method., In this method i am using jQuery $.post() call to submit the form to URL. I am using jQuery to collect all the values entered by the user into form and encode them by calling $("#insertContact :input").serializeArray() method. After the post request control goes to the anonymous function which is third parameter of the $.post() method. In that method i am checking if i got response if yes that means insert was successful if not that means insert failed, that is because my REST service is structured not to send anything back in case of insert failure.


Anonymous said...

it shows this error- "Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at This can be fixed by moving the resource to the same domain or enabling CORS."

what to do?

Anonymous said...

$.support.corps = true;
use before ajax call.