title: Cross-Domain ajax to REST server with jQuery
body: <p>I'm creating a mobile app using PhoneGap. So effectively I'm writing the app in jQuery Mobile, HTML and CSS.</p>
  
  <p>Here's the problem: The server is a REST implementation. This means that to create a new 'entity' (e.g. user, car, hot babe), the server listens for POST requests. If the server will respond to GET requests by returning existing 'entities'.</p>
  
  <p>How does one get the mobile app to send a POST request from jQuery? This sounds simple, but keep in mind that jQuery will change a jsonp datatype request to a GET, even if you specify POST. Also, keep in mind that the server and the app is not on the same domain, so this request is send cross domain, so setting the datatype to json will not work.</p>
  
  <p>I have been stuck with this for 4 long hours. I can get it to work in the blink of an eye if I only care about GET requests, but getting a POST on the server has proven to be impossible so far.</p>
  
  <p>Here is a simplification of the server-side code:</p>
  
  <pre><code> class GatewayService extends Service[Request, Response] {
     def apply(request: Request) = {
       request.method -&gt; Path(request.path) match {
         case GET -&gt; Root / "car" =&gt; {
           println("---Get car---")
         }
         case POST -&gt; Root / "car" =&gt; {
           println("---New car---")
         }
         case _ =&gt; {
           println("---Other method type ---")
        }
      }
  }
  </code></pre>
  
  <p>And here's the jQuery code:</p>
  
  <pre><code>function serverRequest(formId, url) {
      var result = ""
      var formData = readFormData(formId);
      console.log("Submitting form:\n"+JSON.stringify(formData));
  
      $.ajax({
          type:"POST",
          url: url,
          data: JSON.stringify(formData),
         dataType: "json",
          success: function(response){
              alert(response['message']);
              console.log("Received response: "+JSON.stringify(response));
              result = response;
           },
           error: function(jqXHR, textStatus, errorThrown){
               console.log(
                   "The following error occured: "+ jqXHR,
                   textStatus, errorThrown
               );
           },
           complete: function(){
               //Nothing here for now
           }
      });
      return result;
  }
  </code></pre>
  
  <p>If you have any ideas on getting a POST request cross domain with jQuery, please share your secret.</p>
  
  <p>It appears that you cannot simply access a REST server directly from a mobile app developed on jQuery Mobile. Is this correct?</p>