【发布时间】:2014-02-08 19:24:09
【问题描述】:
我有一个跨域请求在 firefox、chrome 等中完美运行 一旦我用 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 config.xml:
<access origin="*" subdomains="true" />
但它仍然没有改变任何东西
知道为什么这不起作用吗?
最好的问候,
杰森
【问题讨论】:
-
在开始请求之前尝试等待 deviceready 事件。
-
你知道什么......这可能会奏效。我会在这件事上回复你。
-
这在模拟器中有效,但当我在 android 设备上运行时无效?