2014-02-08 137 views
0

我,只要我编译的PhoneGap它停止工作Ajax请求在浏览器中工作,但不是在PhoneGap的

阿贾克斯有一个跨域请求,在Firefox完美的工作,铬等 :

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" /> 

但它仍然没有改变任何东西

任何想法,为什么这不工作?

最好的问候,

杰森

+1

尝试你之前等待deviceready事件开始请求。 – QuickFix

+0

你知道什么...可能工作。生病回到你身上。 – Mystx

+0

这在模拟器中工作,但不是当我在Android设备上运行它? – Mystx

回答

0

在PhoneGap的使用Ajax,则必须列入白名单特定域,您可以在这里阅读所有关于它 click here to visit page

+0

我已经这样做了。我已经将白名单添加到了config.xml文件中,并且它不起作用 – Mystx

相关问题