0

我有两页。
html 页面:form 和 jquery ajax 函数将数据发布到 php
php 页面:接收数据并使用 curl 将它们发送到服务器
简而言之:
html,ajx -data-> php -curl-> server
所有这些都运行良好.
现在我将它们移到phonegap中。由于相同的来源策略,我无法将它们发送到另一个域。然后我改用jsonp。

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;
?>

这是行不通的。我无法收到警报消息。有人能告诉我发生了什么吗?或者,这是将数据发布到另一个域服务器的任何其他方式吗?
干杯!

4

2 回答 2

0

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.

于 2013-10-22T14:35:39.283 回答
0

请注意,您必须将您的域列入白名单或放置 * 才能访问所有人,并且在浏览器中开发时会引发跨域错误,当 phonegap 在手机上部署时不会发生错误。

您可以尝试在 google chrome 中使用--disable-web-security标志或使用JSONP在桌面上进行开发

于 2013-10-22T14:34:19.473 回答