发送POST表单数据以JSON格式通过Ajax使用jQuery

编程入门 行业动态 更新时间:2024-10-26 02:25:10
本文介绍了发送POST表单数据以JSON格式通过Ajax使用jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

正如标题所说,我通过AJAX发送一些POST数据。但我一直收到错误,任何人都可以看看在code,并解释为什么我的ajax调用保持失败?

as the title says I'm sending some post data via ajax. but I keep on getting errors, can anyone take a look at the code and explain why my ajax call keeps failing?

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调用返回错误,所以它只是没有成功。不知道为什么。

The ajax call returns the error, so it's just not succeeding. Don't know why.

推荐答案

您发送的数据为一个数组,而不是一个JSON字符串。

Your Sending data as an Array, not a JSON string.

您想要做这样的事情。

$("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!"); } });

更多推荐

发送POST表单数据以JSON格式通过Ajax使用jQuery

本文发布于:2023-06-06 10:50:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/539028.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   格式   POST   JSON   jQuery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!