【问题标题】:javascript array to string JSONjavascript数组到字符串JSON
【发布时间】:2012-07-09 11:02:17
【问题描述】:

首先我是 php 和 javascript 的新手

我有一个网络表单,用户可以在其中添加多个联系人并将其发送到服务器。

由于某些原因,我不能使用普通的 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 选项上,我可以将此选项作为参数。

当我这样做时,我收到以下错误

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

它适用于 jQuery 中的 $.POST

所以我尝试使用JSON.stingify() 将数据转换为字符串。

但是在服务器上我是这样的

'contactArray' => string '[{"name":"username","sex":"12","sex":"false"}]'

如果我使用json_decode,那么我不能使用表单验证。

我想在 CODEIGNITOR 中使用表单验证库。

CI 支持元素数组的验证。

所以

如果我得到类似name[],age[],sex[] 的信息,那么我可以轻松验证。

请帮我解决问题或给我建议。

谢谢。

【问题讨论】:

  • data:[contactArray:contacts] 不是有效的 JavaScript。你的意思可能是data: {contactArray:contacts}
  • 我正在查看这样的数据选项 options["data"] = {contactsArray:contacts};

标签: php javascript arrays json codeigniter


【解决方案1】:

此代码不会创建数组:

tempArray = {
      name:"username",
      age:12,
      sex:false
     };

它创建一个对象(完全覆盖您之前分配给tempArray 的空白数组)。

如果我得到类似 name[],age[],sex[] 的信息,那么我可以轻松验证。

如果您愿意,您可以创建类似这样的发布数据,作为发送数据之前的准备步骤。这很容易::

function prepContacts(contacts) {
    var result = []; // Build up string in array, we'll join at the end
    var nameKey, ageKey, sexKey;

    // I've put [] in these because you use PHP
    nameKey = encodeURIComponent("name[]");
    ageKey = encodeURIComponent("age[]");
    sexKey = encodeURIComponent("sex[]");

    for (index = 0; index < contacts.length; ++index) {
        contact = contacts[index];
        result.push(nameKey + "=" + encodeURIComponent(contact.name));
        result.push(ageKey + "=" + encodeURIComponent(contact.age));
        result.push(sexKey + "=" + encodeURIComponent(contact.sex));
    }

    return result.join("&");
}

然后通过$.ajax发布:

$.ajax({
    url: "/path/to/resource",
    type: "POST",
    data: prepContacts(contacts),
    success: function(response) {
        // POST worked, but you have to check response for whether
        // it worked at the logic level
    },
    error: function() {
        // POST failed
    }
});

【讨论】:

  • 非常感谢..但作为一个新手我必须问这个.. :) 所以首先我需要创建一个联系人对象..然后转换为数组,对吗?
  • @Garbage:上面没有将其转换为数组,而是将其转换为 POST 数据字符串,其中包含姓名、年龄和性别的并行条目。 (例如,name[0]、age[0] 和 sex[0] 都用于同一个联系人。)您将继续按原样构建您的联系人列表(对象数组),然后使用它将其发送到服务器时的准备步骤。
  • 是的,我知道了..但是我从服务器得到了奇怪的结果..0 =&gt; string 'n' (length=1) 1 =&gt; string 'a' (length=1) 2 =&gt; string 'm' (length=1) 3 =&gt; string 'e' (length=1) 4 =&gt; string '%' (length=1) 5 =&gt; string '5' (length=1) 6 =&gt; string 'B' (length=1) 7 =&gt; string '%' (length=1) 8 =&gt; string '5' (length=1) 9 =&gt; string 'D' (length=1) 10 =&gt; string '=' (length=1) 11 =&gt; string '1' (length=1)
  • @Garbage:抱歉,我不明白为什么会发生这种情况,但我既不是 PHP 大佬,也不是 CodeIgniter 的用户。
  • Crowder 非常感谢您,先生,这不是 php 的问题。我用 $.post 测试了这个并且效果很好..但是当我使用 malsup 时,我得到了这样的结果..这很重要问题..但我从你那里学到了很多..非常感谢..
猜你喜欢
  • 2015-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-08
  • 2013-03-29
  • 2016-10-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多