4

jquery ajax 呼び出しを介して json オブジェクトの配列を php ファイルに送信します。

var arr = new Array();
var record1 = {'a':'1','b':'2','c':'3'};
var record2 = {'d':'4','e':'5','f':'6'};
arr.push(record1);
arr.push(record2);

jquery ajaxを介して配列を送信するにはどうすればよいですか? どうすればphpで値を取得できますか? ありがとう。

4

4 に答える 4

15
$.ajax({
        url: "api",
        type: "POST",
        dataType: 'json',
        data: JSON.stringify(arr),
        success: function(response){}

       });

そしてPHPで:

$strRequest = file_get_contents('php://input');
$Request = json_decode($strRequest);
于 2013-01-25T14:05:13.383 に答える
2

JSON.stringify()役に立つかもしれないと思います。

または、json_decode()phpファイルで使用できます。

于 2013-01-25T14:02:05.123 に答える
1

まず、プラグイン:jquery.json-2.4.jsをダウンロードしてプロジェクトに追加します。このプラグインはあなたの人生を楽にするたくさんのヘルパーを提供します。

次に、$。ajaxで、データを使用します:$ .toJSON(arr)、

于 2013-01-25T14:06:26.903 に答える
1

最初に知っておかなければならないことは、作業を簡単にするために 2 つのファイルを用意する必要があるということです。1. php コードを配置する場所 phpcode.php 2. ajax コードを配置する場所 ajax.html ajax コードを配置するページで、jquery プラグインに接続していることを確認します。

 <script> 
          $(document).ready(function(){
      // your ajax code here  delete mine and put your codes
    $.getJSON(' phpcode.php ', function(data) {
              $('#myJson').html('<table style="color:red"><tr><td>' + data.name + '</td><td>' + data.user + '</td></tr></table>');
            });
          });

        </script>
    <body>
    <!—You may put the bellow div in the ajax page so that u load your data in it ---->
        <div id="myJson"></div>
      </body>
    </html>
     In your php page you need something like this at the end of your code 
    // The JSON standard MIME header.
    header('Content-type: application/json');
    echo  json_encode($array);
    Note: I took an example from my working codes just to save the time but I think you get the Idea
于 2013-01-25T14:48:10.917 に答える