AJAX post在服务器端(PHP)是空的

AJAX post is empty on the server side (PHP)

本文关键字:PHP post 服务器端 AJAX      更新时间:2023-09-26

我正在使用jQuery ajax向PHP控制器发出AJAX请求,但是当尝试使用PHP获取发布的数据时,$_POST是空的。下面是实际功能:

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) {  }
    });                         
}

提前感谢!

首先,您不需要字符串化数据。只需发送对象文字即可。

data: {manufacturer: selectedmanufacturer},

其次,您不需要此行:

contentType: "application/json",

让 jQuery 为你做编码:

$.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) {  }});