1
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 storeSettingsObj = JSON.stringify( {'widget':storeSettings} );

                        /* Place it in the storage(only if needed) */
                        if(getKeySettings != storeSettingsObj){
                            //alert(storeSettingsObj);
                            var memberfilter = new Array();
                            memberfilter[0] = "id";
                            memberfilter[1] = "style";
                            var jsonText = JSON.stringify(storeSettings, memberfilter);
alert(jsonText);


   // it gives this result 
[{"id":"widget1"},{"id":"widget3","style":"black"},{"id":"widget4"},{"id":"widget5"},{"id":"widget2","style":"black"},{"id":"widget6"},{"id":"widget7"},{"id":"widget9"},{"id":"widget8"},{"id":"widget10"},{"id":"widget12"},{"id":"widget13"},{"id":"widget11"},{"id":"widget14"}]

では、jquery ajaxを介して送信するにはどうすればよいですか?下記のコードを試していますが、サーバーでデータを取得できません

$.ajax({
    type: 'POST',
    url: 'get_query.php',                      
    data: "test="+jsonText,
    success: function(data) {
        alert(data);
        }
});

では、このデータを送信する方法と取得する方法を説明していただけますか?json_decodeを試しましたが、機能しません。

xdazzコメントの後に編集して、文字列化せずに送信します

print_r($ _ POST ['test'])のときにこれを取得しました

Array

([0] =>配列([id] => widget1[title]=>非表示のウィジェット[hidden]=>1 [collapsed] => 0)

[1] => Array
    (
        [id] => widget3
        [style] => red
        [title] => Icons
        [hidden] => 0
        [collapsed] => 0
    )

[2] => Array
    (
        [id] => widget4
        [style] => black
        [title] => No toggle button
        [hidden] => 0
        [collapsed] => 0
    )

[3] => Array
    (
        [id] => widget5
        [title] => No delete button
        [hidden] => 0
        [collapsed] => 0
    )

[4] => Array
    (
        [id] => widget6
        [title] => No edit button
        [hidden] => 0
        [collapsed] => 0
    )

[5] => Array
    (
        [id] => widget7
        [title] => Not fullscreen button
        [hidden] => 0
        [collapsed] => 0
    )

[6] => Array
    (
        [id] => widget9
        [title] => No custom button
        [hidden] => 0
        [collapsed] => 0
    )

[7] => Array
    (
        [id] => widget8
        [title] => Not sortable
        [hidden] => 0
        [collapsed] => 0
    )

[8] => Array
    (
        [id] => widget10
        [title] => Collapsed widget
        [hidden] => 0
        [collapsed] => 1
    )

[9] => Array
    (
        [id] => widget12
        [title] => Auto refresh
        [hidden] => 0
        [collapsed] => 0
    )

[10] => Array
    (
        [id] => widget2
        [style] => black
        [title] => Basic widget
        [hidden] => 0
        [collapsed] => 0
    )

[11] => Array
    (
        [id] => widget13
        [title] => No timestamp
        [hidden] => 0
        [collapsed] => 0
    )

[12] => Array
    (
        [id] => widget11
        [title] => Ajax
        [hidden] => 0
        [collapsed] => 0
    )

[13] => Array
    (
        [id] => widget14
        [title] => No refresh button
        [hidden] => 0
        [collapsed] => 0
    )

)。

4

3 に答える 3

5

オブジェクトを文字列化する必要はなく、dataプロパティに直接使用します。

$.ajax({
    type: 'POST',
    url: 'get_query.php',                      
    data: {test: myvariable},
    success: function(data) {
      alert(data);
    }
});

次に、php側で、var_dump($_POST['test']);何が得られるかを確認します。

于 2012-10-05T06:19:59.070 に答える
2

あなたはこれを試すことができます:

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

ただし、PHP側では、$GLOBALS["HTTP_RAW_POST_DATA"]が必要になります。JSONデータを受信します。

于 2012-10-05T06:22:27.183 に答える
0
<script>
var information = ['hi','hello'];
$.ajax({
    type: "POST",
    data: {info: information, "action": "getUserRecords"},
    url: "/var/www/pinboard/wp-content/themes/pinboard/userinfo.php",
    success: function(msg) {
        $('.userDetail').html(msg);
    }
});
</script>
于 2016-01-04T05:28:03.583 に答える