Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/466.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在jQueryAjax中设置原始主体?_Javascript_Jquery_Post - Fatal编程技术网

Javascript 如何在jQueryAjax中设置原始主体?

Javascript 如何在jQueryAjax中设置原始主体?,javascript,jquery,post,Javascript,Jquery,Post,我需要发送一个JSON字符串作为POST请求的主体,而不是发送一个键/值对列表。 我使用jQuery的$.ajax函数发出此POST请求。 如何正确设置 当我说JSON字符串时,我的意思是:{action:'x',params:['a','b','c']} 以下是我在服务器上使用PHP中的JSON字符串的方式: var_dump(json_decode(file_get_contents('php://input'))); 结果: stdClass Object action = x

我需要发送一个JSON字符串作为POST请求的主体,而不是发送一个键/值对列表。
我使用jQuery的$.ajax函数发出此POST请求。
如何正确设置

当我说JSON字符串时,我的意思是:
{action:'x',params:['a','b','c']}

以下是我在服务器上使用PHP中的JSON字符串的方式:

var_dump(json_decode(file_get_contents('php://input')));
结果:

stdClass Object
    action = x
    params = Array
        (
    0 = a
    1 = b
    2 = c
        )

如果你不指定的关键,我想它会张贴为没有关键一样的身体

$.ajax({
data:JSON.stringify({action:'x',params:['a','b','c']})
});
尝试:

希望这有帮助


Pete

我现在明白了-您想用自己选择的json字符串替换xhr请求的主体吗?这里不知道-希望专家会过来告诉我这是否可能是“contentType”:“application/json”在我看来。你的语法很奇怪——请看——但processData确实起到了作用。我的语法也有7年了。:)
$.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.
});