我在下面给出了XMLHttpRequest()函数

var searchFriendRequests = function (userid) {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', 'http://localhost:6344/api/Registeration/searchFriendrequests?userid=' + userid, false);
    xhr.setRequestHeader("Content-Type", "text/xml");
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4) {
            if (xhr.status == 200) {
                var data = xhr.responseText;
            }
        }
    };
    xhr.send(null);
}


其中xhr.responseText返回值为

{
    "$id": "1",
    "ContentEncoding": null,
    "ContentType": null,
    "Data": [
        {
            "$id": "2",
            "email": "[email protected]"
        },
        {
            "$id": "3",
            "email": "[email protected]"
        }
    ],
    "JsonRequestBehavior": 1,
    "MaxJsonLength": null,
    "RecursionLimit": null
}


如何从Data获取responseText字段?

最佳答案

使用JSON.parse(),例如:

var data=xhr.responseText;
var jsonResponse = JSON.parse(data);
console.log(jsonResponse["Data"]);

关于javascript - 如何从xhr.responseText获取“数据”字段?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43732717/

10-11 12:39