views:

149

answers:

5

I've noticed that when using $.post() in jquery that the default contentType is application/x-www-form-urlencoded - when my asp.net mvc code needs to have contentType=application/json

(See this question for why I must use application/json: http://stackoverflow.com/questions/2792603/aspnet-mvc-why-is-modelstate-isvalid-false-the-x-field-is-required-when-that)

How can I make $.post() send contentType=application/json? I already have a large number of $.post() functions, so I don't want to change to $.ajax() because it would take too much time

If I try

$.post(url, data, function(), "json") 

It still has contentType=application/x-www-form-urlencoded. So what exactly does the "json" param do if it does not change the contenttype to json?

If I try

$.ajaxSetup({
  contentType: "application/json; charset=utf-8"
});

That works but affects every single $.get and $.post that I have and causes some to break.

So is there some way that I can change the behavior of $.post() to send contentType=application/json?

+3  A: 

I think you may have to modify the source to make $.post always use JSON data type as it really is just a shortcut for a pre configured $.ajax call. Or you could define your own utility function that is a shortcut for the Ajax configuration you want to use, or thirdly, you could overwrite the $.post function with your own implementation.

The JSON datatype in your example refers to the datatype returned from the server and not the format sent to the server.

Russ Cam
+1, I would go for defining a new method, or overwriting `jQuery.post` method, it's a [really simple function](http://github.com/jquery/jquery/blob/1.4.2/src/ajax.js#L143)...
CMS
Its not a bad idea, just create a method called $.mvcpost() that does the same as $.post (by copying the linked code) plus changes the contenttype. Then for all the $.post()s that need to be changed, I just have to type 3 extra characters in front. Its much quicker than rewriting them as $.ajax().
JK
I have implemented this and its working well. No side effects so far!
JK
A: 

You can't send application/json directly -- it has to be a parameter of a GET/POST request.

So something like

$.post(url, {json: "...json..."}, function());
Coronatus
A: 

use just

jQuery.ajax ({
    url: myurl,
    type: "POST",
    data: mydata,
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    success: function(){
        //
    }
});

UPDATED @JK: If you write in your question only one code example with $.post you find one corresponding example in the answer. I don't want to repeat the same information which you already studied till know: $.post and $.get are short forms of $.ajax. So just use $.ajax and you can use the full set of it's parameters without having to change any global settings.

By the way I would you don't recommend to overwrite the standard $.post. It's my personal opinion, but for me it's important, not only that the program works, but also that all who read your program understand it with the same way. Overwriting standard methods without having a very important reason can follow to misunderstanding in reading of the program code. So I repeat my recommendation one more time: just use the original $.ajax form jQuery instead of jQuery.get and jQuery.post and you receive programs which not only perfect work, but can be read by people without any misunderstanding.

Oleg
A: 
$.ajax({
  url:url,
  type:"POST",
  data:data,
  contentType:"application/json; charset=utf-8",
  dataType:"json",
  success: function(){
    ...
  }
})

See : jQuery.ajax()

Adrien Friggeri
why was I downvoted ??
Adrien Friggeri
re-read the original post.
x1a4
The original post asks : "So is there some way that I can change the behavior of $.post() to send contentType=application/json?" BUT it also states "That works but affects every single $.get and $.post that I have and causes some to break.". I understand the question as "how can I achieve the same thing as using $.post but sending the right contentType without breaking the other occurrences of $.get and $.post". Is that incorrect ?
Adrien Friggeri
+1  A: 

The "json" datatype that you can pass as the last parameter to post() indicates what type of data the function is expecting in the server's response, not what type it's sending in the request. Specifically it sets the "Accept" header.

Honestly your best bet is to switch to an ajax() call. The post() function is meant as a convenience; a simplified version of the ajax() call for when you are just doing a simple form posting. You aren't.

If you really don't want to switch, you could make your own function called, say, xpost(), in and have it simply transform the given parameters into parameters for a jQuery ajax() call, with the content-type set. That way, rather than rewriting all of those post() functions into ajax() functions, you just have to change them all from post to xpost (or whatever).

JacobM
Its only the $.post() methods that call an asp.net mvc controller method that need to change. The pure jquery ones should be unchanged (autocomplete, diaplog, jqgrid etc) I was hoping there would a simple change that I could make to the relevant $.post()s. But it does look like I need to convert them to $.ajax(). Its a large and very ajax heavy app, so there are a lot of them to change.
JK