Search code examples
javascriptjqueryjquery-post

not sure why this jquery post isn't working


I have the following jquery:

  var xj=[{"name":"person","id":1},{"name":"jack", "id":2}];
  $.post('/hex-jt/locations',xj , function(data){
    console.log("this posted");
  },'json');

which seems like it should be ok. But it is passed like this to my rails app:

enter image description here

Any idea what is going on with this?


Solution

  • You are calling jquery.post() with bad argument data, passing an array instead of a String or a PlainObject.

    jQuery.post( url [, data ] [, success(data, textStatus, jqXHR) ] [, dataType ] )
    
    data
    Type: PlainObject or String
    A plain object or string that is sent to the server with the request.
    

    You can for instance modify it like this, wrapping the array in an object:

    xj={"users":[{"name":"person","id":1},{"name":"jack", "id":2}]};