[英]How do you send a raw strings through a jQuery POST request?
I am trying to POST a raw string via jQuery.ajax() 我试图通过jQuery.ajax()发布一个原始字符串
eg, 例如,
contact_list=352345
I have 我有
$.ajax({
beforeSend: function(xhr){
xhr.setRequestHeader('Content-Type', header);
}
},
url: link,
type: type,
processData:false,
data: data,
success: function(data){
console.log(data);
}
});
Most of the time I am sending JSON data, so header='application/json'
大多数时候我发送JSON数据,所以
header='application/json'
On the server side, I echo $HTTP_RAW_POST_DATA
and see my JSON string just fine. 在服务器端,我回显
$HTTP_RAW_POST_DATA
并看到我的JSON字符串就好了。
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. 但是当我设置
header='application/x-www-form-urlencoded'
$HTTP_RAW_POST_DATA
为空。
ProcessData
is false, so shouldn't it just pass my string on through? ProcessData
是false,所以它不应该只是通过我的字符串?
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. 作为临时解决方法,我只是将标题保留为
application/json
并忽略服务器上针对此特定端点的Content-Type
。
No extra work needed. 无需额外的工作。 Try this:
尝试这个:
$.ajax({
url: link,
data: data,
success: function(data){
console.log(data);
}
});
processDataBoolean:
processDataBoolean:
Default: true By default, data passed in to the data option as an object (technically, anything other than a string) will be processed and transformed into a query string, fitting to the default content-type "application/x-www-form-urlencoded".
默认值:true默认情况下,作为对象传入数据选项的数据(技术上,不是字符串)将被处理并转换为查询字符串,适合默认内容类型“application / x-www-form -urlencoded”。 If you want to send a DOMDocument, or other non-processed data, set this option to false.
如果要发送DOMDocument或其他未处理的数据,请将此选项设置为false。
I would rather send a flag to my javascript function to see what sort of input 我宁愿发送一个标志到我的javascript函数,看看有什么样的输入
so your function will look like this 所以你的功能看起来像这样
function processIt(request_type,link,type,data) {
if(request_type == 'json')
$.ajax({
beforeSend: function(xhr){
xhr.setRequestHeader('Content-Type', header);
}
},
url: link,
type: type,
processData:false,
data: data,
success: function(data){
console.log(data);
}
});
else
$.ajax({
url: link,
data: data,
success: function(data){
//do something
}
});
}
But that's if you want to use your function as it is. 但是,如果您想要使用您的功能,那就是这样。
PHP $HTTP_RAW_POST_DATA
is not available with enctype="multipart/form-data". PHP
$HTTP_RAW_POST_DATA
不适用于enctype =“multipart / form-data”。 http://www.php.net/manual/en/ini.core.php#ini.always-populate-raw-post-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.'&';
}
}
Seems to be working. 似乎工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.