Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 访问Dojo请求上的http正文错误_Javascript_Ajax_Dojo_Request - Fatal编程技术网

Javascript 访问Dojo请求上的http正文错误

Javascript 访问Dojo请求上的http正文错误,javascript,ajax,dojo,request,Javascript,Ajax,Dojo,Request,我的Web服务在http正文中提供了有关occoured错误的详细信息。 如何在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 require(["dojo/dom", "

我的Web服务在http正文中提供了有关occoured错误的详细信息。 如何在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
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;
            });
        });
    }
);
我的Dojo请求如下所示:

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
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;
            });
        });
    }
);
我想要的是正文中的文本:

This is my exception!

当我将dojo用于Ajax请求时,error方法总是有多个参数。我认为第一个参数是发送的请求,第二个参数是响应或异常


尝试向方法中添加第二个参数,看看是否包含所需的参数。

看看我的答案

使用
deferred.response。然后
而不是
deferred。然后

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上查看它的实际操作:

您的意思是这样的:函数(错误,测试){alert(测试);}这将发出警报:undefinedok这意味着没有传递第二个参数,所以我上面的注释是错误的。我刚刚注意到您的内容类型设置为application/json,但您的正文是纯文本。不确定这是否是一个问题,但可以在某个地方开始调试