繁体   English   中英

Ajax请求可在浏览器中工作,但不能在phonegap中工作

[英]Ajax request working in browsers but not in phonegap

我有一个跨域请求,只要我使用phonegap编译,它就可以在Firefox,Chrome等浏览器中完美运行

阿贾克斯:

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

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

知道为什么这不起作用吗?

最好的祝福,

杰森

在phonegap中使用ajax,您必须将特定域列入白名单,您可以在此处阅读有关其的所有信息, 请单击此处访问页面

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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