javascript - phonegap XMLHttpRequest responseText 在 android 中为空,但在 iphone 中包含数据

标签 javascript android iphone ajax cordova

我正在 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 上工作......

最佳答案

检查跨域脚本问题。我做了几乎同样的事情,只是我的 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

关于javascript - phonegap XMLHttpRequest responseText 在 android 中为空,但在 iphone 中包含数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8270385/

相关文章:

javascript - Sharepoint OnlineexecuteQueryAsync JSOM - 返回值

Android:如何通过蓝牙浏览远程设备文件系统?

iphone - 我怎样才能使导航栏的颜色成为我想要的任何颜色?

javascript - 在javascript中使用正则表达式检测表情符号

javascript - 在 fullpage.js 上添加 onLeave 会中断滚动

android - CardView的优缺点

android - 对 ListView 项目实现 OnClickListener

iphone - 自定义 UITabBar 选定的标签图像,iOS 5 及更高版本

iphone - 实现对 iphone/ipad 的传真支持

javascript - 当通过 JS 修改 <div> 背景时,CSS 动画停止工作