jQuery ajax - xhr.responseText始终未定义

时间:2015-09-02 08:29:42

标签: javascript jquery ajax

我正在尝试调试对API的jQuery调用,但xhr.responseText似乎总是未定义。我需要对此代码进行哪些更改才能查看错误的详细信息?

jQuery.ajax({
    url: 'http://fusion-api-user.dev/api/v1/user/authorise/' + currentVideoItem.id + '/play',
    async: async,
    beforeSend: function(xhr){
        xhr.setRequestHeader('X-Forwarded-Host', 'fusion.dev');
    },
    xhrFields: {
        withCredentials: true
    },
    success: function (res, status, xhr) {
        currentVideoItem.authModule.playWithToken(xhr.getResponseHeader("X-Token"), "ais");
        alert('xhr: ' + xhr);
        alert('token:' + xhr.getResponseHeader('X-Token'));

        jQuery(currentVideoItem.parentContainer).trigger('video-authorized', [{
            id: currentVideoItem.id
        }]);
    },
    error: function(xhr, status) {
        alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
        alert("responseText: " + xhr.responseText);
        jQuery(window).trigger('countdown-session-ended', [{
            name: 'video', 
            trigger: 'authorizePlayError'
        }]);
    }
});

1 个答案:

答案 0 :(得分:0)

我通过添加回调函数并在成功函数中调用它来解决这个问题,如下所示:

success: function (response) {
//
    callback(response);
//
},