我的jQuery.ajax请求PHP后端中的POST数据无法获取 [英] My jQuery.ajax request POST data in PHP backend can not get
本文介绍了我的jQuery.ajax请求PHP后端中的POST数据无法获取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用我的jQuery ajax请求PHP后端:
I use my jQuery ajax to request PHP backend:
jQuery.ajax({
url: url,
type: "POST",
data: {
qn_action: 'bmc',
action: 'productdetails',
id: 772,
server_name: 'LL-01',
tblclients_id: 1,
bmc_action: 'on',
},
dataType: 'json',
contentType: "application/json; charset=utf-8",
success: function (data, textStatus){
console.log('ldl:',data)
},
error: function(jqXHR, e) {
var msg = '';
if(jqXHR.status==0){
msg = 'You are offline!!\n Please Check Your Network.';
}else if(jqXHR.status==404){
msg = 'Requested URL not found.';
}else if(jqXHR.status==500){
msg = 'Internal Server Error.<br/>'+jqXHR.responseText;
}else if(e=='parsererror'){
msg = 'Error: Parsing JSON Request failed.';
}else if(e=='timeout'){
msg = 'Request Time out.';
}else {
msg = 'Unknow Error.<br/>'+x.responseText;
}
console.log('error: '+jqXHR.responseText);
console.log('Error msg: '+msg);
}
})
在PHP后端:
我要检查请求数据:
echo json_encode(
parse_str($_POST['qn_action'])
);
在浏览器控制台中将得到 ldl:–空
.
there will get ldl: – null
in the browser console.
如果我使用
echo json_encode(
$_POST['qiyun02_action']
);
仍然获取 ldl:–空
.
如果我使用
echo json_encode(
$_POST
);
我将得到 ldl:[]
.
那么,如何从jQuery ajax请求中获取数据?
so, how can I get the data from jQuery ajax request?
推荐答案
在您的PHP代码中,可能缺少json标头.
In your PHP code probably missing header for json.
header('Content-Type: application/json');
因为您已经在jquery中定义了 JSON
Because you have defined JSON
in your jquery
dataType: 'json',
因此服务器期望json结果.正如文档所说
so server expect json result. As docs say
您希望从服务器返回的数据类型.
The type of data that you're expecting back from the server.
这篇关于我的jQuery.ajax请求PHP后端中的POST数据无法获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文