javascript - Sending JSON as a parameter value for post -


here's i'm trying accomplish:

  1. user enters json in textarea element.

    {     "test":[         {"a":"b"}     ] }        
  2. client side javascript parses json.

    myobject = $.parsejson($("#my-textarea").val()); 
  3. json sent on ajax post request server with

    datatype: json, data: {"my_object": myobject} 
  4. 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

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -