AJAX响应未定义

时间:2018-11-15 13:26:50

标签: javascript ajax xmlhttprequest

我已经检查了有关该主题的各种帖子,但看不到为什么我仍然无法为responseText定义。返回的json应该看起来像{“ token”:“ ghargaeorigjaoregrjarjegijra [pgjpraejgprjgpkfp5p34i5483te8q9rut053”}

function getAuth(username, password) {

    let http = new XMLHttpRequest();
    let url, method;
    let data = {"username": username, "password": password};

    url = returnLocation("default");
    method = 'POST';

    http.open(method, "http://" + url + "/monitor/admin/auth/", true);
    http.setRequestHeader("Content-Type", "application/json");
    http.setRequestHeader("Accept", "application/json");
    http.send(JSON.stringify(data));

    http.onreadystatechange = function() {
        if (http.readyState === XMLHttpRequest.DONE && http.status === 200){
            setCookie(http.responseText);
        }
        else if (http.readyState === XMLHttpRequest.DONE && http.status !== 200){
            alert(http.statusText);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我用的是http.response而不是http.ResponseText