我的网络服务将有关发生的错误的详细信息放在 http 正文中。
我如何在 dojo 请求中访问此详细信息。
例如,http 错误如下所示:
HTTP/1.1 500 Internal Server Error
Transfer-encoding: chunked
Content-type: application/json
Date: Tue, 18 Sep 2012 18:47:31 GMT
15
This is my exception!
0
我的 Dojo 请求如下所示:
require(["dojo/dom", "dojo/on", "dojo/request",
"dojo/json", "dojo/domReady!"],
function(dom, on, request, JSON){
// Results will be displayed in resultDiv
var resultDiv = dom.byId("errorResult");
// Attach the onclick event handler to the makeRequest button
on(dom.byId('errorButton'),"click", function(evt){
request.get("./rest/test/error", {
// Parse data from JSON to a JavaScript object
handleAs: "json"
}).then(function(data){
resultDiv.innerHTML = "Username: " + data.name + "</br>Role:" + data.role;
},
function(error){
// Display the error returned
resultDiv.innerHTML = error;
});
});
}
);
显示的错误是:
RequestError: Unable to load ./rest/test/error status: 500
我想要的是正文中的文本:
This is my exception!
看看我的回答如何检索 AMD 化 Dojo 的 XHR 响应代码(+时间戳)? https://stackoverflow.com/questions/12154748/how-to-retreive-xhr-response-code-timestamp-of-amdized-dojo/12155418#12155418
Use deferred.response.then
代替deferred.then
:
var deferred = request.get("./rest/test/error", { handleAs: "json" });
deferred.response.then(
// success
function(response) {
console.log("data:", response.data); // parsed json
console.log("http body:", response.text); // raw text
},
// error
function(error) {
var response = error.response;
console.log("http body:", response.text);
}
);
在 jsFiddle 上查看它的实际效果:http://jsfiddle.net/phusick/SGh5M/ http://jsfiddle.net/phusick/SGh5M/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)