访问xmlHttpRequest的响应,给出错误

时间:2018-04-10 12:37:38

标签: jquery xmlhttprequest

我正在运行xmlHttpRequest。请求的响应是一个在其中具有名称值对的对象。其中一个名称值对看起来像这样:

"actor":{id:"", ed:""}

我正在尝试访问此名称值对作为响应。但我无法做到。我是这样做的。

 var xhr = new XMLHttpRequest();
    xhr.withCredentials = true;

xhr.addEventListener("readystatechange", function () {
    var thaaat = this;
    if (this.readyState === 4) {
        let resp = xhr.responseText
        console.log(JSON.parse(xhr.responseText).actor.id)
        let id;
        if(document.getElementById("occupation-input").value.toUpperCase() === "POLITICIAN"){
            id = thaaat.response.politician.id
        }
        else if(document.getElementById("occupation-input").value.toUpperCase() === "ACTOR"){
            id = thaaat.response.actor.id
        }
        console.log("success")
        that.afterFaceAdded(id);
    }
});

xhr.open("POST",url);
xhr.setRequestHeader("authorization", "Token " + that.props.token_Reducer.token);
xhr.setRequestHeader("content-type", "application/json");
//xhr.setRequestHeader("cache-control", "no-cache");
//xhr.setRequestHeader("postman-token", "87ea9499-816d-f5e6-ae2b-f190bc268b79");

xhr.send(data);

然而它给我一个错误说:

  

AddTagSection.js:268 Uncaught TypeError:无法读取属性' id'未定义的           在XMLHttpRequest.eval

响应看起来像这样

enter image description here

0 个答案:

没有答案