>我有一个跨域请求在火狐、铬等中完美运行一旦我使用 Phonegap 编译它就会停止工作
阿贾克斯:
function createCORSRequest(method, url){
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr){
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined"){
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
xhr = null;
}
return xhr;
}
var request = createCORSRequest("get", "http://www.sellfast.co.za/get.php");
if (request){
request.onload = function(){
document.body.innerHTML = request.responseText;
}
request.send();
}
此代码重写正文 hmtl,仅显示从服务器获取的结果文本。我这样做只是为了测试。一旦在PhoneGap中运行,它就什么都不做
这是服务器上的 get.php:
<?php
header("Access-Control-Allow-Origin: *");
echo "Hello world";
?>
我将其添加到 phonegap 配置中.xml:
<access origin="*" subdomains="true" />
但它仍然没有改变任何东西
知道为什么这不起作用吗?
此致敬意
杰森
在 Phonegap 中使用 ajax,您必须将特定域列入白名单,您可以在此处阅读所有相关信息点击这里访问页面