javascript - Sending JSON as a parameter value for post -
here's i'm trying accomplish:
user enters json in textarea element.
{ "test":[ {"a":"b"} ] }
client side javascript parses json.
myobject = $.parsejson($("#my-textarea").val());
json sent on ajax post request server with
datatype: json, data: {"my_object": myobject}
post parameters checked on server side in sinatra , json looks now.
{ "test": { "0": { "a": "b" } } }
i'm wondering why test array changed hash , if there's can avoid that. i'm thinking original json improperly formatted, i'm unsure.
edit: here stripped down version of ajax request , controller action.
function test() { return $.ajax({ url: "/test", type: "post", datatype: "json", data: {"test":[{"a":"b"}]}, success: function(response) { }, error:function(jqxhr,exception) { ajaxerror(jqxhr,exception); } }) } post '/test' puts params return {} end
i stringify resulting json object before send it, this:
datatype: json, data: {"my_object": json.stringify(myobject)}
if have support browsers don't have json natively, can conditionally import json js add support. (jquery not natively have json stringify method).
Comments
Post a Comment