Phonegap http GET请求返回空字符串

时间:2014-01-21 14:28:55

标签: javascript php xmlhttprequest phonegap-build

所以,我正在尝试从我的服务器server.com/api调用Phonegap Android App中的一些数据。我通过HTTP Get方法调用它。

localhostfile://甚至server.com上,一切正常。我还将我的域名添加到config.xml

现在代码:

服务器PHP

header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');

JS

var xhr = new XMLHttpRequest();
xhr.open("GET", "http://server.com/api", true);
xhr.onreadystatechange = function(){
  if ( xhr.readyState == 4 ) {
    if ( xhr.status == 200 || xhr.status == 0 ) {
      alert(xhr.responseText);
    } else {
      alert("NOPE");
    }
  }
};
xhr.send(null);

的config.xml

<access origin="http://server.com"/>

<preference name="phonegap-version" value="3.1.0" />

欢迎任何想法,

干杯!

修改

我确实为origin =“”尝试了一堆组合(例如“”,“ .server.com”等)

1 个答案:

答案 0 :(得分:0)

这里要尝试的是将访问源更改为

<access origin="http://server.com/*"/>


我在一些PhoneGap应用程序上遇到了这个问题,它最终有助于添加* ...

相关问题