jQuery - How to PUT JSON via Ajax?

前端 未结 3 970
感动是毒
感动是毒 2020-12-01 07:25

I am trying to put some JSON formatted data via Ajax with jQuery to a server. My code looks like this:

$.ajax({
    type: \"PUT\",
    url: myURL,
    conten         


        
3条回答
  •  爱一瞬间的悲伤
    2020-12-01 07:44

    If you always have to send JSON in your application, then you can just execute this somewhere in your init and then use default $.ajax call as in your example, and it will always serialize as a JSON string instead of the Ajax default query string.

    Here I use the JSON object mentioned above:

    $.ajaxSetup({
        contentType : 'application/json',
        processData : false
    });
    $.ajaxPrefilter( function( options, originalOptions, jqXHR ) {
        if (options.data){
            options.data=JSON.stringify(options.data);
        }
    });
    

提交回复
热议问题