使用jQuery发送JSON数据时获取空数组

时间:2012-10-04 07:52:08

标签: json jquery

以下是我的代码:

注意到storeSettings是从localStorage中检索的。

var storeSettings = [];

obj.find(o_widgetClass).each(function(){
    var storeSettingsStr          = {};
    storeSettingsStr['id']        = $(this).attr('id');
    storeSettingsStr['style']     = $(this).attr('data-widget-attstyle');
    storeSettingsStr['title']     = $(this).children('header').children('h2').text();
    storeSettingsStr['hidden']    = ($(this).is(':hidden') ? 1 : 0);
    storeSettingsStr['collapsed'] = ($(this).hasClass('powerwidget-collapsed') ? 1 : 0);
    storeSettings.push(storeSettingsStr);
    });

 var memberfilter = new Array();
    memberfilter[0] = "id";
    memberfilter[1] = "style";

var jsonText = JSON.stringify(storeSettings, memberfilter);

//when i alert data, this is the output string i get if before sending it :
/*
 [{"id":"widget1"},{"id":"widget2"},{"id":"widget3","style":"black"},{"id":"widget4","style":"purple"},{"id":"widget5"},{"id":"widget6"},{"id":"widget7","style":"black"},{"id":"widget9"},{"id":"widget8","style":"black"},{"id":"widget10"},{"id":"widget11"},{"id":"widget12"},{"id":"widget13"},{"id":"widget14"}]
 */
 alert(jsonText);

$.ajax({
        type: 'POST',
        url: 'get_query.php',
        //dataType: 'json',
        //contentType: 'json',
        data: jsonText,
        success: function(data) {
            alert(data);
         }
     });

在get_query.php上,我刚刚打印出来了:

但它没有显示任何内容,只是给了我一个空数组。

Array()

1 个答案:

答案 0 :(得分:0)

我的这个问题的答案已经解决了:

how to send JSON array through ajax?