AJAX帖子在服务器端是空的(PHP)
问题描述:
I am making an AJAX request to a PHP controller, by using jQuery ajax, but when trying to get the posted data with PHP the $_POST is empty. Below is the actual function:
function GetSeriesForManufacturer(manuf) {
selectedmanufacturer = manuf;
//Make an AJax Call For Getting Series Of Manufacturer
var series = null;
$.ajax({
type: "POST",
url: url,
data: "{manufacturer:'" + selectedmanufacturer + "'}",
contentType: "application/json", //; charset=utf-8",
dataType: "json",
cache: false,
async: false,
success: function (response) {
//remove loading gif
$(".loading").hide();
//Append Data
AppendSeries($.parseJSON(response.text), selectedmanufacturer);
//Custom Scrollbar Call
$('.MatchingSeries ul').mCustomScrollbar();
},
error: function (XMLHttpRequest, textStatus, errorThrown) { }
});
}
Thanks in advance!
我正在使用jQuery ajax向PHP控制器发出AJAX请求,但在尝试获取发布的数据时 使用PHP,$ _POST为空。 以下是实际功能: p>
function GetSeriesForManufacturer(manuf){
selectedmanufacturer = manuf;
//为获取制造商系列制作一个AJax调用
var series = null;
$ .ajax({
type:“POST”,
url:url,
data:“{manufacturer:'”+ selectedmanufacturer +“'}”,
contentType:“application / json” ,//; charset = utf-8“,
dataType:”json“,
cache:false,
async:false,
success:function(response){
// remove remove gif
$ (“.loading”)。hide();
//追加数据
AppendSeries($。parseJSON(response.text),selectedmanufacturer);
//自定义滚动条调用
$('。匹配系列ul') .mCustomScrollbar();
},
错误:函数(XMLHttpRequest,textStatus,errorThrown){}
});
}
code> pre>
谢谢 提前! p>
div>
答
First, you don't need to stringify data. Just send object literal is ok.
data: {manufacturer: selectedmanufacturer},
Second, you don't need this line:
contentType: "application/json",
答
Let jQuery do the encoding for you:
$.ajax({
type: "POST",
url: url,
data: {
manufacturer: selectedmanufacturer
},
contentType: "application/json", //; charset=utf-8",
dataType: "json",
cache: false,
async: false,
success: function (response) {
...
},
error: function (XMLHttpRequest, textStatus, errorThrown) { }});