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

时间:2022-10-09 00:23:12

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?

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

3 个解决方案

#1


1  

Try this it may help make the 'id' which is sent to the server as a global variable and access it

尝试一下,它可能有助于将作为全局变量发送到服务器并访问它的“id”

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

#2


1  

Although this is an old question i have the exact same challenge as Jodes

虽然这是一个老问题,但我有和朱迪一样的挑战

I found that using this inside the error function holds the ajax object and from that you can extract the data (this.data) although its url encoded

我发现在error函数中使用这个函数可以保存ajax对象,并从中提取数据(this.data),尽管它的url是经过编码的

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

Hope this helps

希望这有助于

edit: I used https://gist.github.com/brucekirkpatrick/7026682 for unserializing the data string

编辑:我使用https://gist.github.com/brucekirkpatrick/7026682对数据字符串进行反序列化

#3


1  

You can access the data property of the ajax options inside the error handler with just this.data.
If you don't specifically set the context property, this inside the callbacks for $.ajax will always be the ajax call itself, and then this.data will be the serialized string of the data you passed in.

您可以使用this.data在错误处理程序中访问ajax选项的数据属性。如果您没有特别设置上下文属性,则在回调中为$设置此属性。ajax始终是ajax调用本身,然后是这个。数据将是您传入的数据的序列化字符串。

$.ajax({
    type  : 'POST',
    url   : 'addToBasket.php',
    data  : 'id='+id,
    error : function(xhr, textStatus, error){
        var data = this.data; // "id=something"
    },
    success : function(data, textStatus, xhr){}
});

FIDDLE

小提琴

If you need to parse that string into an object, here's how -> How can I get query string values in JavaScript?

如果需要将该字符串解析为一个对象,下面是如何——>如何在JavaScript中获取查询字符串值?

You could also use the global error handler to get the data object, as it the ajax call as it's first argument.
It will fire on all ajax errors, but can be filtered by URL or any other option passed etc.

您还可以使用全局错误处理程序来获取数据对象,因为它是作为第一个参数的ajax调用。它将对所有的ajax错误进行攻击,但是可以通过URL或其他通过的选项进行过滤。

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

FIDDLE

小提琴

#1


1  

Try this it may help make the 'id' which is sent to the server as a global variable and access it

尝试一下,它可能有助于将作为全局变量发送到服务器并访问它的“id”

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

#2


1  

Although this is an old question i have the exact same challenge as Jodes

虽然这是一个老问题,但我有和朱迪一样的挑战

I found that using this inside the error function holds the ajax object and from that you can extract the data (this.data) although its url encoded

我发现在error函数中使用这个函数可以保存ajax对象,并从中提取数据(this.data),尽管它的url是经过编码的

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

Hope this helps

希望这有助于

edit: I used https://gist.github.com/brucekirkpatrick/7026682 for unserializing the data string

编辑:我使用https://gist.github.com/brucekirkpatrick/7026682对数据字符串进行反序列化

#3


1  

You can access the data property of the ajax options inside the error handler with just this.data.
If you don't specifically set the context property, this inside the callbacks for $.ajax will always be the ajax call itself, and then this.data will be the serialized string of the data you passed in.

您可以使用this.data在错误处理程序中访问ajax选项的数据属性。如果您没有特别设置上下文属性,则在回调中为$设置此属性。ajax始终是ajax调用本身,然后是这个。数据将是您传入的数据的序列化字符串。

$.ajax({
    type  : 'POST',
    url   : 'addToBasket.php',
    data  : 'id='+id,
    error : function(xhr, textStatus, error){
        var data = this.data; // "id=something"
    },
    success : function(data, textStatus, xhr){}
});

FIDDLE

小提琴

If you need to parse that string into an object, here's how -> How can I get query string values in JavaScript?

如果需要将该字符串解析为一个对象,下面是如何——>如何在JavaScript中获取查询字符串值?

You could also use the global error handler to get the data object, as it the ajax call as it's first argument.
It will fire on all ajax errors, but can be filtered by URL or any other option passed etc.

您还可以使用全局错误处理程序来获取数据对象,因为它是作为第一个参数的ajax调用。它将对所有的ajax错误进行攻击,但是可以通过URL或其他通过的选项进行过滤。

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

FIDDLE

小提琴