[英]Array not sent by jQuery ajax
I am trying to send an array using the jQuery ajax function, but it isn't working. 我正在尝试使用jQuery ajax函数发送数组,但是它不起作用。
Here is my code: 这是我的代码:
if (section_name == "first_details_div") {
var fields_arr = ["f_name", "l_name", "identity_number", "kupat_holim_id", "kupat_holim_insurance_ID", "birth_date", "father_name", "family_status_id"];
var section_values = new Array();
for (i = 0; i < fields_arr.length; i++) {
if (document.getElementById(fields_arr[i]))
section_values[fields_arr[i]] = document.getElementById(fields_arr[i]).value;
}
}
var array_to_send = $.serialize(section_values);
$.post("ajax_save_intek_section.php", {
section_name: section_name,
section_values: array_to_send
},
function(data) {
alert('here!');
if (data) {
alert(data); //"<?=getstring('saved_successfully')?>"
}
});
I tried to add this line before (as I saw in other answer): 我曾尝试添加此行(如我在其他答案中所见):
var array_to_send = $.serialize(section_values);
but it doesn't recognize this function. 但无法识别此功能。
Any ideas? 有任何想法吗?
Try to use var array_to_send= $(section_values).serialize();
尝试使用
var array_to_send= $(section_values).serialize();
stead. 代替。 but you also can create an object and serialize it with JSON.sttringify(object);
但您也可以创建一个对象并使用JSON.sttringify(object);进行序列化;
var request = {
section_name: section_name,
section_values: section_values
};
$.post("ajax_save_intek_section.php", JSON.stringify(request),
function(data){
//rest of your code here
}
});
//Array of STRINGS
var fields_arr = ["f_name", "l_name", "identity_number", "kupat_holim_id",
//Array that takes INTEGER as an index
var section_values = new Array();
for (i = 0; i < fields_arr.length; i++) {
if (document.getElementById(fields_arr[i]))
//giving the STRING as an INDEX to the array instead of INTEGER
section_values[fields_arr[i]] = document.getElementById(fields_arr[i]).value;
}
}
you are doing something like 你正在做类似的事情
section_values["f_name"]=document.getElementById(fields_arr[i]).value;
which will keep your section_values as an empty array. 这会将您的section_values保留为空数组。 I think you wanted to do this instead
我想你想这样做
section_values[i]=document.getElementById(fields_arr[i]).value;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.