JQuery的:AJAX错误:获取发送到服务器的数据发送到、错误、服务器、数据

2023-09-10 18:06:49 作者:明月心

这应该是简单的,但我无法找到的信息就如何做到这一点的任何地方:

This should be simple, but I can't find info anywhere on how to do it:

$.ajax({
    type: 'POST',
    url: 'addToBasket.php',
    data: 'id='+id,
    // ....
    error: function(xhr, textStatus, error){
        // how to get the id sent to the server?
    },
    success: function(data, textStatus, xhr){}
});

如何能在错误处理功能的访问请求发送到服务器中的数据?

How can the error handling function access the data sent to the server in the request?

推荐答案

您可以用全局错误处理程序来获取数据对象。 这将触发对所有的Ajax错误,但可以通过网址或通过任何其他选项

You can use the global error handler to get the data object. It will fire on all ajax errors, but can be filtered by URL or any other option passed

$(document).ajaxError(function(event, jqXHR, ajaxSettings, thrownError) {
    if (ajaxSettings.url == 'addToBasket.php') {
        alert(ajaxSettings.data)
    }
});

FIDDLE