通过AJAX动态发送POST表单数据JSON格式与jQuery [英] Send post form data in json format via ajax with JQuery dynamically
问题描述
我徜徉在JSON格式通过AJAX使用jQuery动态如何发送POST表单数据? 例如,我编码JQ是这样的:
I wander how send post form data in json format via ajax with JQuery dynamically? For example I'm coding something like this in JQ:
$.post("test.php", { func: "getNameAndTime" },
function(data){
alert(data.name); // John
console.log(data.time); // 2pm
}, "json");
这很好,但在现场的应用程序经常需要发送大量表单数据和用户可以动态改变的领域,所以我不知道有多少FUNC1,FUNC2,FUNC3甚至FUNC []将被发送。 在Q是如何动态地做到这一点,在旧世界的AJAX我可以做它通过serealizing的形式发送到服务器。 感谢名单提前。
and that's good, but in live apps often need to send huge form data and user can dynamically change the fields, so I don't know how many func1, func2, func3 or even func[] will be sent. The q is how to do this dynamically, in old world of an AJAX I could done it by serealizing the form and send to the server. Thanx in advance.
推荐答案
是的,我可以把所有的数据到服务器,并在任何情况下,它会运行良好, 例如:
Yes I can send all the data to the server and in any case it will worked well, example:
$(function() { // on document load
$('#email_form').submit(function() { // set onsubmit event to the form
var data = $('#email_form').serialize(); // serialize all the data in the form
$.ajax({
url: 'testJson.php', // php script to retern json encoded string
data: data, // serialized data to send on server
dataType:'json', // set recieving type - JSON in case of a question
type:'POST', // set sending HTTP Request type
async:false,
success: function(data) { // callback method for further manipulations
for (key in data.email) {
alert(data.email[key]);
}
},
error: function(data) { // if error occured
}
});
return false;
});
});
这篇关于通过AJAX动态发送POST表单数据JSON格式与jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!