如何在jQuery Ajax中设置原始主体? [英] How to set the raw body in jQuery Ajax?
本文介绍了如何在jQuery Ajax中设置原始主体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不需要发送键/值对列表,而是需要发送一个JSON字符串作为POST请求的主体。
我使用jQuery的$ .ajax函数发出此POST请求。 >
如何正确设置?
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.
How do I set it correctly?
当我说JSON字符串时,我的意思是: {action:'x' ,params:['a','b','c']}
。
When I say JSON string, I mean something like: {action:'x',params:['a','b','c']}
.
这就是我将如何使用这个JSON字符串服务器上的PHP:
This is how I would use this JSON string in PHP on the server:
var_dump(json_decode(file_get_contents('php://input')));
结果:
stdClass Object
action = x
params = Array
(
0 = a
1 = b
2 = c
)
推荐答案
尝试:
$.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.
});
希望这会有所帮助,
Pete
这篇关于如何在jQuery Ajax中设置原始主体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文