如何通过jQuery POST请求发送原始字符串? [英] How do you send a raw strings through a jQuery POST request?
问题描述
我正在尝试通过jQuery.ajax()发布原始字符串
I am trying to POST a raw string via jQuery.ajax()
例如
contact_list=352345
我有
$.ajax({
beforeSend: function(xhr){
xhr.setRequestHeader('Content-Type', header);
}
},
url: link,
type: type,
processData:false,
data: data,
success: function(data){
console.log(data);
}
});
大多数是我发送JSON数据的时间,所以header='application/json'
Most of the time I am sending JSON data, so header='application/json'
在服务器端,我回显$HTTP_RAW_POST_DATA
并看到我的JSON字符串就好了.
On the server side, I echo $HTTP_RAW_POST_DATA
and see my JSON string just fine.
但是,有时我也想发送普通表格数据.但是当我设置header='application/x-www-form-urlencoded'
$HTTP_RAW_POST_DATA
为空时.
However, I sometimes want to send normal form data too. But when I set header='application/x-www-form-urlencoded'
$HTTP_RAW_POST_DATA
is empty.
ProcessData
是错误的,所以它不应该只是通过我的字符串吗?
ProcessData
is false, so shouldn't it just pass my string on through?
作为临时的解决方法,我只是将标头保留为application/json
,而忽略服务器上该特定端点的Content-Type
.
As a temporary workaround I'm just leaving the header as application/json
and ignoring the Content-Type
on the server for this particular endpoint.
推荐答案
PHP $HTTP_RAW_POST_DATA
不适用于enctype ="multipart/form-data". http://www.php .net/manual/en/ini.core.php#ini.always-populate-raw-post-data
PHP $HTTP_RAW_POST_DATA
is not available with enctype="multipart/form-data". http://www.php.net/manual/en/ini.core.php#ini.always-populate-raw-post-data
因此,现在在服务器上,请执行以下操作:
So, now on the server I do the following:
if (count($_POST == 0){
$raw = file_get_contents('php://input');
}else{
$raw = '';
foreach($_POST as $key => $value) {
$raw = $raw.$key.'='.$value.'&';
}
}
似乎正在工作.
这篇关于如何通过jQuery POST请求发送原始字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!