【发布时间】:2015-04-07 04:21:59
【问题描述】:
所以我正在尝试将同步 xml 请求重写为异步 xml 请求。我对此很陌生,所以如果错误真的很愚蠢,请原谅我。
无论如何,这就是我目前所拥有的。
var getEmployeeData = function(section, question, column){
var xhr = new XMLHttpRequest();
xhr.open("GET", Data.contextPath + "/main/?action=get-employee-list§ion=" +section+ "&question=" +question+ "&column=" +column, true)
xhr.onload = function (e) {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
if (employeeDataDone) {
if (xhr.responseText==("\r\n")) {
employeeList = null;
employeeInverseList = null;
} else {
employeeList = JSON.parse(xhr.responseText)[0];
employeeInverseList = JSON.parse(xhr.responseText)[1];
}
}
} else {
console.error(xhr.statusText);
}
}
}.bind(this);
xhr.onerror = function (e) {
console.error(xhr.statusText);
};
xhr.send(null);
}.bind(this);
感谢您提供任何帮助,如果您需要任何我忽略的额外信息,请直接询问。
谢谢!
【问题讨论】:
-
为什么要检查 onload 里面的 4 和 200?
-
@epascarello 我不知道,对不起,我对此很陌生。可以去掉吗?
-
@epascarello 他在 XHR 对象上使用了错误的方法名称。在请求的整个生命周期中以不同的状态多次调用正确的方法名称。
标签: javascript xml servlets asynchronous request