简体   繁体   中英

Post data with cross-domain in PhoneGap

I have two pages.
html page: form and jquery ajax function to post data to php
php page: receive data and use curl to send them to a server
In short:
html,ajx -data-> php -curl-> server
All of them are working pretty well.
Now I move them into phonegap. because of the same origin policy, I can not send them to another domain. Then I use jsonp instead.

var data = "test";
$.ajax({
    type: "GET",
    dataType: "jsonp",
    url: "http://xx.xx.com/xx/receive.php?callback="+data,
    success: function(data){
       alert(data);
    }
}
============================================================
receive.php:
<?php
   $abc = $_GET["callback"];
   echo $abc;
?>

This is not working. I can not get the alert message.Could someone tell what's going on? Or, is that any other way to post data to another domain server?
Cheers!

Be aware that you have to whitelist your domains or put * to access everyone, also the cross domain error are thrown where developing in a browser, when phonegap deploys on the phone the errors don't happen.

You could try using --disable-web-security flag in google chrome or use JSONP to develop on desktop

you have specified dataType: "jsonp"
that means that you are expecting that server will return jsonp data

from official jquery ajax documentation :
"dataType (default: Intelligent Guess (xml, json, script, or html))
Type: String
The type of data that you're expecting back from the server. If none is specified, jQuery will try to infer it based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string)."

try removing dataType parameter.
Or you can put error callback to alert the error. I think then you can trace error comfortably.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM