dsbowmvth16379079 2012-07-18 07:55
浏览 461
已采纳

通过ajax发送多维数组

So basically here is my jQuery and HTML code for creating arrays. It basically takes each div with class setting and checks wether it has span with attribute rel, if it has, it inserts in array under array. Each new div.settign span rel attributes will be inserted to another array under same parent array.

Check this code to understand it - http://jsfiddle.net/xVuHx/ .

Pasted it also here -

jQuery(document).ready(function() {
    var rel = [];
    jQuery(".setting").each(function() {
        rel.push(jQuery(this).find('span[rel]').map(function() {
            return this.getAttribute('rel');
        }).get());
    });               
    jQuery("body").text(rel);
});​

and html -

<div class="setting">
    <span rel="Variable">Variable</span>
    <span rel="Item">Item</span>
    <span rel="Something">Something</span>
</div>
<div>
    <span rel="Smth">Smth</span>
    <span>Sec</span>
</div>
<div class="setting">
    <span>Second</span>
    <span rel="first">First</span>
    <span rel="Third">Third</span>
</div>

it's working great and displays two arrays - one with 3 elements and one with two, but when I try to pass it to PHP file via ajax, ti gives me [object Object] with var_dump($array); . Also I tried to do foreach, but it told me that invalid arguments were passed to foreach statement, so I guess I'm sending the array to ajax incorrectly or the Array is made incorrectly.

Here is my ajax code -

var myArray = jQuery(this).sortable("serialize") + "&type="+rel+"&action=update_homepage";
jQuery.post("'.admin_url("admin-ajax.php").'", myArray, function(response){
   var info = response.slice(0,-1);
});

This ajax code is made via wordpress, so its ajax requests differs a little bit from default AJAX requests.

Could you please check what exactly could be the problem?

EDIT:

Just tried to output with print_r($myArray); in php side, it outputs empty string.

  • 写回答

2条回答 默认 最新

  • dongtou5557 2012-07-18 08:08
    关注

    Check format of posted data in AJAX request in browser console (section network).

    You can find information about opening the console in your browser in this stackexchange question.

    JSON-js - JSON in JavaScript.

    To convert an object to JSON, use JSON.stringify:

    var json_text = JSON.stringify(your_object, null, 2);
    

    Encode your array to JSON and then send it with Ajax request. To decode JSON in PHP use json_decode

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 spring后端vue前端
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题