How to pass multi-dimensional array with Jquery AJAX post?

前端 未结 6 1142
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-31 17:17

I\'ve been using Serialize() to pass checkbox form data with Post() for a basket that can hold multiple items of the same category.

When I post them using the submit

6条回答
  •  半阙折子戏
    2020-12-31 17:44

    I did not find any good solution, so i solved this using JSON.stringify(); here is my code

    Client side :

    var data = {a:{'foo':'bar'},b:{'this':'that'}};
    $.ajax({ url        : '/',
             type       : 'POST',                                              
             data       : {'data':JSON.stringify(data)},
             success    : function(){ }
           });
    

    Server Side:

    $data = json_decode($_POST['data']);
    print_r($data);
    // Result:
    // Array( "a" => Array("foo"=> "bar"), "b" => Array("that" => "this"))
    

提交回复
热议问题