How to set the raw body in jQuery Ajax?

后端 未结 2 1494
一整个雨季
一整个雨季 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: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

提交回复
热议问题