xmlHttp.responseText不能从json webservice返回数据

xmlHttp.responseText does not return data from json webservice

本文关键字:webservice 返回 数据 json responseText 不能 xmlHttp      更新时间:2023-09-26

我试图从json webservice检索数据。

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

我没有得到var结果。当我用包含json对象的文本文件替换webservice时,我可以检索json对象作为responseText。请帮助

重要的事情先说…永远,永远,永远不要使用eval *eval = evil.

如何使用GET与AJAX…

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();

如何使用POST与AJAX…

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);