正如标题所示,我正在通过 ajax 发送一些帖子数据。但我不断收到错误,任何人都可以看一下代码并解释为什么我的 ajax 调用不断失败吗?
submitForm(jQuery('#priceCalc'), {name: 'thingdoto', value: "true"});
function submitForm(form, data) {
var postData = form.serializeArray(),
formURL = form.attr("action");
postData.push(data);
console.log(postData);
jQuery.ajax({
url : formURL,
type: 'POST',
dataType : "json",
data: postData,
success:function(data)
{
jQuery('#priceTotal').html(data);
},
error: function()
{
jQuery('#priceTotal').html('error');
}
});
}
编辑:ajax 调用返回错误,因此它没有成功。不知道为什么。
您以数组形式发送数据,而不是 JSON 字符串。
你想做这样的事情。
$("form#ID").submit(function(e){
e.preventDefault();
var data = {}
var Form = this;
//Gathering the Data
//and removing undefined keys(buttons)
$.each(this.elements, function(i, v){
var input = $(v);
data[input.attr("name")] = input.val();
delete data["undefined"];
});
//Form Validation goes here....
//Save Form Data........
$.ajax({
cache: false,
url : ?,
type: "POST",
dataType : "json",
data : JSON.stringify(data),
context : Form,
success : function(callback){
//Where $(this) => context == FORM
console.log(JSON.parse(callback));
$(this).html("Success!");
},
error : function(){
$(this).html("Error!");
}
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)