-1

JSON Web サービスからデータを取得しようとしています。

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

var の結果は何も得られません。webservice を json オブジェクトを含むテキスト ファイルに置き換えると、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 に答える