[英]How to post a form with ajax and return data in array?
HI how to post a form and return data it will be a array as like this 嗨,如何发布表格并返回数据,它将是一个像这样的数组
{
"notes":'some notes',
"validUntil": '12/12/2015',
"status": 1,
"menuItemName": "HR Section",
"menuItemDesc": "gggg"
}
My code is this 我的代码是这样
$('#add-menu-list .btn[data-attr=submit-btn]').on('click', function(){ var formValidate = $('#add-menu-list').parsley().validate(); validateFront(); // console.log(formValidate); var menuName = $('input[data-api-attr=menuItemName]').val(); var validUntil = $('input[data-api-attr=validUntil]').val(); var menuStatus = $('input[data-api-attr=status]').val(); var menuNote = $('textarea[data-api-attr=notes]').val(); var menuDesc = $('textarea[data-api-attr=menuItemDesc]').val(); var dataString = { menuItemName: menuName, validUntil : validUntil, status : menuStatus, notes : menuNote, menuItemDesc : menuDesc }; if(formValidate == true){ alert('success'); console.log(menuName + validUntil + menuStatus + menuNote + menuDesc); var url = "xyz.html"; // the script where you handle the form input. $.ajax({ type: "POST", // url: url, dataType: "json", data: $(dataString).serialize(), // serializes the form's elements. success: function(data) { alert(data); // show response } }); }else{ alert('Validation fail '); } });
Since "data" is a server response i guess that your server return a json object. 由于“数据”是服务器响应,我想您的服务器返回了一个json对象。 In this case you have to somehow inform the jquery's ajax that you expect a json response from server or you have to translate the "data" to a json object by your self.
在这种情况下,您必须以某种方式告知jquery的ajax,您希望服务器发出json响应,或者您必须自行将“数据”转换为json对象。
It is best to follow the first option so you don t have to deal with the translation your self you can easily do that by giving an extra parameter tou your ajax reuest : dataType: 'json',
this will do the trick! 最好遵循第一个选项,这样您就不必自己处理翻译,您可以通过给ajax reuest指定一个额外的参数轻松地做到这一点:
dataType: 'json',
这可以解决问题!
Now that you have a proper response object from your request you can either stringify it with var string_resp=JSON.stringify(data);
现在,您已经从请求中获得了正确的响应对象,您可以使用
var string_resp=JSON.stringify(data);
对其进行字符串化var string_resp=JSON.stringify(data);
and then alert it alert(string_resp)
or you can access its elements like that : alert(data.status)
which will alert your object's status field etc. 然后提醒它
alert(string_resp)
或您可以访问它的元素,例如: alert(data.status)
,它将提醒对象的状态字段等。
so your code will be : 因此您的代码将是:
$.ajax({
type: "POST",
url: url,
dataType: 'json',
data: $(menuName).serialize(), // serializes the form's elements.
success: function(data)
{
alert(data); // will alert an object
alert(data.status); // will alert object's status field in this case 1
alert(JSON.stringify(data)) // will alert the object as a string
}
});
you are sending only one value in serialize, serialize() should be on form element not on field element, like : 您只在serialize中发送一个值,serialize()应该在表单元素上,而不在字段元素上,例如:
$('#add-menu-list .btn[data-attr=submit-btn]').on('click', function(){
...
$.ajax({
...
data:$("#form").serialize();
...
success: function(data)
{
alert(data.notes); // show response
....
}
var myObj = { "notes":'some notes', "validUntil": '12/12/2015', "status": 1, "menuItemName": "HR Section", "menuItemDesc": "gggg" }; myObj.toString = function() { var str = ''; for (var property in myObj) { if (myObj.hasOwnProperty(property) && (property != "toString") ) { str += (property + ': ' + myObj[property] + "\\n"); // do stuff } } return str; } alert(myObj);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.