首先,我是新来的PHP和JavaScriptJavaScript数组串JSON
我有一个web表单,用户可以添加多个联系人ANS发送到服务器疗法。
由于一些原因,我不能使用正常的html元素来存储值,所以我使用数组来存储值。
//init array
var contacts = new Array(); //contact array
var tempArray = new Array(); //temp array to store current contacts
//getting the contact info and setting to a temp array
tempArray = {
name:"username",
age:12,
sex:false
};
//push the content to the `contacts` array
contacts.push(tempArray);
我增加了许多接触到contacts
阵列,现在我需要将阵列提交给服务器。
问题
我使用Codeignitor和Malsup FORM插件。
按malsup我可以配置数据选项,这样
var options = {
dataType:'json', //type of data
data:[contactArray:contacts], //additional parm
};
和ajaxSubmit
选项,我可以给这个选项作为PARM。
当我做到这一点我收到以下错误
uncaught exception: [Exception... "Component returned failure code: 0x80460001 (NS_ERROR_CANNOT_CONVERT_DATA) [nsIDOMFormData.append]" nsresult: "0x80460001 (NS_ERROR_CANNOT_CONVERT_DATA)" location: "JS frame :: /js/form.js :: fileUploadXhr :: line 224" data: no]
temp/jquery.min.js
Line 4
IT jQuery中与$.POST
的作品。
所以我试图JSON.stingify()
将数据转换为字符串。
,但在服务器上我得到这样
'contactArray' => string '[{"name":"username","sex":"12","sex":"false"}]'
如果我使用的json_decode
然后我不能使用表单验证。
我想使用CODEIGNITOR中的FORM VALIDATION LIBRARY。
CI支持验证元素数组。
所以
,如果我得到的东西像name[],age[],sex[]
的话,我可以很容易地验证。
请帮我解决问题或给我建议。
谢谢。
'数据:[contactArray:联系人]'是不是有效的JavaScript。你可能的意思是'data:{contactArray:contacts}' – Utkanos 2012-07-09 11:07:07
我正在查找像这样的选项的数据选项[“data”] = {contactsArray:contacts}; – Red 2012-07-09 11:15:57