Search code examples
jqueryes6-promise

How do I get the real reason why promise is rejected


I have this code

var loadFilePromise = new Promise(function(loadFilePromiseResolve, loadFilePromiseReject) {

    $.ajax({
        type: 'post',
        url: "http://localhost:12343/MyTestPage.aspx",
        data: {
            DocumentFileID: documentFileId
        },
        dataType: 'json'
    }).then(function(resp) {
        // Do something here
        loadFilePromiseResolve();
    }, function(xhr) {
        // This is reject response from ajax. And it hits here.
        var msg = "Error occurred: " + xhr.statusText + " (" + xhr.status + ")\n" + Util.parseAspxExceptionMessage(xhr.responseText);
        loadFilePromiseReject(msg);
    });

});

I would like to know as to know why the promise has been rejected. I looked inside the xhr object and there is none. The weird thing about this is that the status is 200 and the statusText = OK.

Need help on this because I don't have much knowledge yet in ES6.


Solution

  • See the jQuery documentation on ajax:

    jqXHR.then(
        function( data, textStatus, jqXHR ) {},
        function( jqXHR, textStatus, errorThrown ) {}
    );
    

    Incorporates the functionality of the .done() and .fail() methods, allowing (as of jQuery 1.8) the underlying Promise to be manipulated. Refer to deferred.then() for implementation details.

    The signature of the error callback is function(jqXHR, textStatus, errorThrown) so you can get more information about the exact problem using textStatus and errorThrown.