ajax POST不发送部分对象 [英] ajax POST not sending part of object
问题描述
所以我有这个代码:
var ajaxUrl = 'a/7776/as';
var data = {
'answer' : { 'user_input' : [] },
'form_build_id' : 'form-ffe6f10e9601470ed4cfe38257a959a6'
}
$.ajax({
url: ajaxUrl,
dataType: 'json',
type: 'POST',
data: data,
success: function(json){
}
});
然而,当我检查POST来源时,似乎没有发送'回答'..
However when I inspected the POST source it appears that 'answer' is not being sent..
这是Firebug中的POST源:
Here's the POST source in Firebug:
Parametersapplication/x-www-form-urlencoded
form_build_id form-ffe6f10e9601470ed4cfe38257a959a6
Source
form_build_id=form-ffe6f10e9601470ed4cfe38257a959a6
为什么会出现这种情况?如何才能将对象的'answer'部分发送到AJAX帖子?
Why is this the case and how can I get it to also send the 'answer' part of the object into the AJAX post?
推荐答案
来自TJ的好解释克劳德,显然知识渊博。这是我对实际答案的看法。我检查了我的HTTP流量并运行了上面的AJAX请求。正如预期的那样,回答
未被发送。另外上面跟着肾上腺素的回答,如果你在google上搜索ajax不发送空数组,你只能看到相关结果的数量,这也是一个问题。我要做的是,如果 user_input
是一个空数组,请发送 null
。我自己试了一下,确保并且,正如预期的那样,这次我观察了请求中发送的 answer
参数,以及 user_input
使用以下代码发送为null。
Nice explanation from T.J. Crowder, clearly knowledgeable. Here's my take at a practical answer. I inspected my HTTP traffic and ran your above AJAX request. As expected, answer
was not being sent. Also going along with adrenalin's answer above, if you search for "ajax not sending empty array" on google, you can see just by the number of related results that this is also an issue. What I would do is if user_input
is an empty array, send null
instead. I tried it myself to make sure and, again as expected, this time I observed the answer
parameter being sent in the request, along with user_input
which was sent as null with the code below.
var userInput = []; //not sure where this is coming from in reality, but this is just an example.
if (userInput.length == 0){ //in this example, this is obviously always true
userInput = null;
}
var ajaxUrl = 'a/7776/as';
var data = {
'answer' : { 'user_input' : userInput },
'form_build_id' : 'form-ffe6f10e9601470ed4cfe38257a959a6'
}
$.ajax({
url: ajaxUrl,
dataType: 'json',
type: 'POST',
data: data,
success: function(json){
}
});
这篇关于ajax POST不发送部分对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!