从JavaScript发送数组到PHP? [英] send array to php from javascript?

查看:95
本文介绍了从JavaScript发送数组到PHP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用jquery ajax将javascript数组发送到php.

i want to send a javascript array to php using jquery ajax.

$.post("controllers/ajaxcalls/users.php",
{
    links: links
});

其中第二个链接"是一个javascript数组.

where the second 'links' is a javascript array.

当我有这个数组时:

'1' ...
    '1' => "comment1"
    '2' => "link1"
    '3' => "link2"
    '4' => "link3"
'2' ...
    '1' => "comment2"
    '2' => "link4"

然后使用:

var jsonLinks = JSON.stringify(links);
alert(jsonLinks);

会给我的

[null,[null,"comment1","link1","link2","link3"],[null,"comment2","link4"]]

对我来说似乎出了点问题.而且我无法在php端使用json_decode来获取元素.

seems to me that something is wrong. and i cant use json_decode on php side to get the elements.

我应该使用什么函数将其转换为json?如何在php端访问它?现在已经在几个小时内为此感到苦恼了...希望能获得一些帮助.

what function should i use to convert it to json and how do i access it on the php side? have struggled with this in some hours now...would appreciate some help.

推荐答案

我不确定您的示例是否可以工作(您尝试过吗?)如果不能,则可以使用json发送数组,然后对其进行解码与php( http://www.php.net/manual/zh-CN/function.json-decode.php )

I'm not sure if your example will work (did you try it?) If not, you can use json to send your array, and then decode it with php (http://www.php.net/manual/en/function.json-decode.php)

如JSON网站上所述: 字符串化器方法可以采用可选的替换器功能.将在结构中的每个值的toJSON方法(如果有)之后调用它.它将每个键和值作为参数传递给它,并将其绑定到持有键的对象.返回的值将被字符串化.

As written on the JSON website: The stringifier method can take an optional replacer function. It will be called after the toJSON method (if there is one) on each of the values in the structure. It will be passed each key and value as parameters, and this will be bound to object holding the key. The value returned will be stringified.

排除了在JSON中没有表示形式的值(例如函数和未定义的值).

Values that do not have a representation in JSON (such as functions and undefined) are excluded.

非整数将替换为null.要替换其他值,可以使用如下替换函数:

Nonfinite numbers are replaced with null. To substitute other values, you could use a replacer function like this:

function replacer(key, value) {
    if (typeof value === 'number' && !isFinite(value)) {
        return String(value);
    }
    return value;
}

可以在此处找到整页: http://www.json.org/js.html

Full page can be found here: http://www.json.org/js.html

这篇关于从JavaScript发送数组到PHP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆