byte_slave byte_slave - 8 months ago 34
jQuery Question

$.post() doesn't send data as json but as x-www-form-urlencoded instead

This one is really weird. I've multiple

in the code but there is one dunno why sends the json parameters as
instead and therefore doesn't work.

Here's the code:

$.post("/Route/SaveTransportProperties", { properties: JSON.stringify(propArray), currTravelBox: JSON.stringify(travelBoxObj), accessToken: getAccessToken()}, function(data)

The XHR looks like this in Firefox:
Firefox screenshot

Any ideas why is this happening? I also enforced the type as 'json' but doesn't work either.


If you want to send the data as json then use the $.ajax function

You can specify type post and dataType json.

  url: "",
  type: "POST",
  dataType: "xml/html/script/json", // expected format for response
  contentType: "application/json", // send as JSON
  data: $.param( $("Element or Expression") ),

  complete: function() {
    //called when complete

  success: function() {
    //called when successful

  error: function() {
    //called when there is an error

Taken from ajax documentation

Default: 'application/x-www-form-urlencoded; charset=UTF-8'