简体   繁体   English

使用XMLHttpRequest()获取JSON文件,但是当我console.log responseText时,我得到一个空字符串吗?

[英]Getting a JSON file using XMLHttpRequest() but when I console.log the responseText I get an empty string?

When I say console.log(getErrors) I receive this in the console: 当我说console.log(getErrors)时,我在控制台中收到此消息:

XMLHttpRequest {statusText: "", status: 0, responseURL: "", response: "", responseType: ""...}
onabort: null
onerror: null
...
responseText: {"content":[{"id":1,"timeStamp":"2015-03-20T00:01:44.761","provider":"foo","providerId":null,"lineNumber":1,"summary":"foo","description":"foo: 1"}...
responseType: ""
responseURL: "http//localhost:8080/errors/findAll"
...

I know I'm getting the data because I can see it in responseText but when I say console.log(getErrors.responseText) I'm getting an empty string. 我知道我正在获取数据,因为我可以在responseText中看到它,但是当我说console.log(getErrors.responseText)时,我得到的是空字符串。 I'm not sure what I'm doing wrong. 我不确定自己在做什么错。

Javascript: 使用Javascript:

var getErrors = new XMLHttpRequest();

    getErrors.open('GET', '/errors/findAll', true);
    getErrors.send();

    //var response = getErrors.responseText;

    console.log(getErrors);
    console.log(getErrors.responseText);

Here is my working code in Javascript: 这是我的Java工作代码:

getErrors = function(url, callbackFunction) // How can I use this callback?
{
    var requestErrors = new XMLHttpRequest();
    requestErrors.onreadystatechange = function()
    {
        if (requestErrors.readyState == 4 && requestErrors.status == 200)
        {
            callback(requestErrors.responseText); // Another callback here
        }
    }
    requestErrors.open('GET', url);
    requestErrors.send();
}

function callback(data) {
    console.log(data);
}

getErrors('/errors/findAll', callback);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM