如何使用Javascript在URL中传递复杂的JSON对象

时间:2014-04-18 12:12:29

标签: javascript jquery json

我正在尝试在URL中传递复杂的JSON对象

input = { "id": 1, "sd": "123", "filter": { "h": 1,"road": true }, "legs": [{ "id": 1, "x1": -0.001, "y1": 51.122 }, { "id": 2, "x1": -12, "y1": 12 }] };

我尝试过这些功能

 data_str = encodeURIComponent(JSON.stringify(input))

 var urlParam = []

 for (var i in input) {
      urlParam.push(encodeURI(i) + "=" + encodeURI(input_[i]));
 }

Ajax Call

$.ajax({
         url: 'localhost/foo',
         type: 'POST',
         dataType: "json",
         //data: { input: data_str },
         data: { input: urlParam.join("&") },
         success: function (e) {
              alert(e);
         }

       });

0 个答案:

没有答案