我的网络服务提供了有关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!
答案 0 :(得分:3)
查看我对How to retreive XHR response code (+timestamp) of AMD'ized Dojo?
的回答使用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/
答案 1 :(得分:0)
当我使用dojo进行Ajax请求时,error方法总是有多个参数。我认为第一个参数是发送的请求,第二个参数是响应或异常。
尝试在方法中添加第二个参数,看看它是否包含您需要的内容。