-1

我正在尝试从 json webservice 检索数据。

if (xmlHttp.status == 200 || xmlHttp.status == 0)
        {
            var result = xmlHttp.responseText;
            json = eval("(" + result + ")");
        }

我对 var 结果一无所获。当我用包含 json 对象的文本文件替换 web 服务时,我可以将 json 对象检索为 responseText。请帮助

4

1 回答 1

1

首先要做的事情......永远永远永远不要使用eval*eval=邪恶。

如何使用GETAJAX...

try {
    http = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e1) {
    try {
        http = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e2) {
        this.xmlhttp = null;
    }
}
var url = "/uri/of/web-service?val1=Laura&val2=Linney" + Math.random();
var params = "val1=Laura&val2=Linney";
http.open("GET", url, true);

http.onreadystatechange = function() {
    if(http.readyState == 4 && http.status == 200) {
        // we have a response and this is where we do something with it
        json = JSON.parse(http.responseText);
    }
}
http.send();

如何使用POSTAJAX...

try {
    http = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e1) {
    try {
        http = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e2) {
        this.xmlhttp = null;
    }
}
var url = "/uri/of/web-service";
var params = "val1=Laura&val2=Linney";
http.open("POST", url, true);

http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");

http.onreadystatechange = function() {
    if(http.readyState == 4 && http.status == 200) {
        // we have a response and this is where we do something with it
        json = JSON.parse(http.responseText);
    }
}
http.send(params);
于 2012-12-23T04:15:30.660 回答