How to send JSON instead of a query string with $.ajax?

后端 未结 4 2172
一生所求
一生所求 2020-11-22 10:39

Can someone explain in an easy way how to make jQuery send actual JSON instead of a query string?

$.ajax({
    url      : url,
    dataType : \'json\', // I          


        
4条回答
  •  天命终不由人
    2020-11-22 11:11

    While I know many architectures like ASP.NET MVC have built-in functionality to handle JSON.stringify as the contentType my situation is a little different so maybe this may help someone in the future. I know it would have saved me hours!

    Since my http requests are being handled by a CGI API from IBM (AS400 environment) on a different subdomain these requests are cross origin, hence the jsonp. I actually send my ajax via javascript object(s). Here is an example of my ajax POST:

     var data = {USER : localProfile,  
            INSTANCE : "HTHACKNEY",  
            PAGE : $('select[name="PAGE"]').val(), 
            TITLE : $("input[name='TITLE']").val(), 
            HTML : html,
            STARTDATE : $("input[name='STARTDATE']").val(), 
            ENDDATE : $("input[name='ENDDATE']").val(),
            ARCHIVE : $("input[name='ARCHIVE']").val(), 
            ACTIVE : $("input[name='ACTIVE']").val(), 
            URGENT : $("input[name='URGENT']").val(), 
            AUTHLST :  authStr};
            //console.log(data);
           $.ajax({
                type: "POST",
               url:   "http://www.domian.com/webservicepgm?callback=?",
               data:  data,
               dataType:'jsonp'
           }).
           done(function(data){
             //handle data.WHATEVER
           });
    

提交回复
热议问题