【问题标题】:phonegap XMLHttpRequest responseText empty with android but contain data in iphonephonegap XMLHttpRequest responseText 在 android 中为空,但在 iphone 中包含数据
【发布时间】:2011-11-25 14:18:54
【问题描述】:

我正在 PhoneGap+dreamweaver cs5.5 中开发一个应用程序,它调用处理程序 (.ashx) 并返回一个 JSON 字符串。我执行以下操作:

function appReady(){
var ajax = new XMLHttpRequest();
    ajax.open("GET","https://xxxx.xxxxx.com/xxxx/xxxx.ashx?method=GetUser&email=xxx@xxxx.com&pwd=123456",false);
    ajax.send();
    ajax.onreadystatechange=function(){
        if(ajax.readyState == 4) {//Request complete !!
            if (ajax.status == 200 || ajax.status == 0) { // OK response
                alert(ajax.responseText);
                document.getElementById('main').innerHTML = ajax.responseText;
            }
        }
    }
}

当我在 iphone 模拟器上运行应用程序时,我恢复了 json 字符串 responseText,但在 android 模拟器上这是空的。在 iphone 中返回的状态是 200,但在 android 中是 0。 如果问题是 coss-domain 请求,那么在任何平台上都不起作用吗?

不明白为什么wiki的例子:http://wiki.phonegap.com/w/page/42450600/PhoneGap%20Ajax% 20Sample

在两个平台上都可以正常工作,而我的只能在 iphone 上运行 ...

【问题讨论】:

  • 在分配就绪状态处理程序后尝试send()

标签: javascript android iphone ajax cordova


【解决方案1】:

检查跨域脚本问题。我做了几乎相同的事情,除了我的 URL 实际上位于同一个域上。它在 Android 和 PC 上运行良好,但 iphone 拒绝运行。当我从 'https://whatever.whatever.com/foo.asp' 更改为 foo.asp 时 - 它起作用了!

var rootpath;
rootpath = "https://the.same.dam.place/foo.asp";   // did not work!
rootpath = "foo.asp";   // shortening it worked.
function read_foo(whatever)
{
    var url = rootpath + "?whatever=" + whatever;
    xmlHttp = GetXmlHttpObject(stateChanged);
    xmlHttp.open("GET", url , true);
    xmlHttp.send(null);
}
function stateChanged()
{
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    {
        var return_place = document.getElementById('return_place');
        var thetext = xmlHttp.responseText;
        return_place.innerHTML= thetext;
    }
}

另见:

Empty responseText from XMLHttpRequest

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-04
    • 1970-01-01
    • 1970-01-01
    • 2011-06-27
    • 2014-05-28
    • 1970-01-01
    • 2011-09-27
    • 1970-01-01
    相关资源
    最近更新 更多