How to set the raw body in jQuery Ajax?

后端 未结 2 1493
一整个雨季
一整个雨季 2020-12-13 02:18

Instead of sending a list of key/value pairs, I need to send a JSON string as the body of the POST request.
I make this POST request using jQuery\'s $.ajax function.

相关标签:
2条回答
  • 2020-12-13 02:37

    if you dont specify the key i think it will post as the body without the key like

    $.ajax({
    data:JSON.stringify({action:'x',params:['a','b','c']})
    });
    
    0 讨论(0)
  • 2020-12-13 02:41

    Try:

    $.ajax('url',{
        'data': JSON.stringify(yourJSONObject), //{action:'x',params:['a','b','c']}
        'type': 'POST',
        'processData': false,
        'contentType': 'application/json' //typically 'application/x-www-form-urlencoded', but the service you are calling may expect 'text/json'... check with the service to see what they expect as content-type in the HTTP header.
    });
    

    Hope this helps,

    Pete

    0 讨论(0)
提交回复
热议问题