简体   繁体   English

通过ajax发送多维数组

[英]Sending multidimensional array via ajax

So basically here is my jQuery and HTML code for creating arrays. 所以基本上这是我用于创建数组的jQuery和HTML代码。 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. 基本上,每个div具有类设置,并检查是否具有属性rel的span,如果具有,则将其插入array下的array中。 Each new div.settign span rel attributes will be inserted to another array under same parent array. 每个新的div.settign span rel属性将插入到同一父数组下的另一个数组。

Check this code to understand it - http://jsfiddle.net/xVuHx/ . 检查此代码以了解它-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 - 和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); 它工作得很好,并显示两个数组-一个包含3个元素,一个包含2个元素,但是当我尝试通过ajax将其传递到PHP文件时,ti给我[object Object]和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. 我也尝试做foreach,但是它告诉我无效的参数传递给了foreach语句,所以我想我将数组错误地发送到ajax或数组被错误地制作了。

Here is my ajax code - 这是我的ajax代码-

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. 此ajax代码是通过wordpress制作的,因此其ajax请求与默认AJAX请求略有不同。

Could you please check what exactly could be the problem? 您能检查一下到底是什么问题吗?

EDIT: 编辑:

Just tried to output with print_r($myArray); 刚刚尝试输出与print_r($myArray); in php side, it outputs empty string. 在php端,它输出空字符串。

Check format of posted data in AJAX request in browser console (section network). 在浏览器控制台(网络部分)中检查AJAX请求中已发布数据的格式。

You can find information about opening the console in your browser in this stackexchange question . 您可以在此stackexchange问​​题中找到有关在浏览器中打开控制台的信息。

JSON-js - JSON in JavaScript. JSON-js -JavaScript中的JSON。

To convert an object to JSON, use JSON.stringify : 要将对象转换为JSON,请使用JSON.stringify

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

Encode your array to JSON and then send it with Ajax request. 将数组编码为JSON,然后与Ajax请求一起发送。 To decode JSON in PHP use json_decode 要在PHP中解码JSON,请使用json_decode

最好将其转换为json

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM