Tuesday, April 17, 2012

Jquery - How to make $.post() use contentType=application/json?


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?


Source: Tips4all

5 comments:

  1. 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.

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


    See : jQuery.ajax()

    ReplyDelete
  3. 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).

    ReplyDelete
  4. 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.

    ReplyDelete
  5. Finally I found the solution, that works for me:

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

    ReplyDelete