Panos
Panos

Reputation: 13

AJAX post is empty on the server side (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!

Upvotes: 0

Views: 312

Answers (2)

Leo
Leo

Reputation: 13838

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",

Upvotes: 1

Buri
Buri

Reputation: 71

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

Upvotes: 0

Related Questions