user2218877
user2218877

Reputation: 11

Passing data in $.ajax request returns an empty array. What is wrong?

I am struggling with this.

I am trying to pass element data to my php file using $.ajax and it always returns empty.

Here is my code:

    strJson = '{'
    $.each(this_side.data(), function(i, v) {
        strJson += i + ":'" + v + "',";
    });
    strJson += term_id + ":'" + term_id + "',";
    strJson += new_page_num + ":'" + new_page_num + "'";
    strJson += '}';

    alert(strJson); // this works

    $.ajax({
        url: 'my url here',
        type: 'post',
        data: strJson,
        success: function(data){
            alert(data);
        }
    });

Outpot of $_POST is an empty array. It's not working.

What I am missing here please???

Upvotes: 1

Views: 808

Answers (5)

lordvlad
lordvlad

Reputation: 5347

don't bother with dataType and contentType, jQuery should to that on its own. what does this_side.data() return? if it is an array, then your i variable in your $.each loop is a number, and your JSON is simply wrongly formatted. try

$.each(this_side.data(), function(i, v) {
    strJson += "'" + i + "':'" + v + "',";
});

and then you should also post a JSON object, not a JSON string:

 $.ajax({
    data : JSON.parse( strJson ),
    ...
 })

this takes also into account when this_side.data() returns an object and the keys have 'special' characters like '-'.

if this_side.data() returns an object, the following is way easier:

$.ajax({
    url: 'my url here',
    type: 'post',
    data: $.extend( 
            this_side.data(),
            {'term_id': term_id, 'new_page_num' : new_page_num }
          ),
    success: function(data){
        alert(data);
    }
});

Upvotes: 1

radu florescu
radu florescu

Reputation: 4363

Updated answer due to lordVlad comment:

strJson = '{'
    $.each(this_side.data(), function(i, v) {
        strJson += i + ":'" + v + "',";
    });
    strJson += "'"+ i + "':'" + v + "',";
    strJson += new_page_num + ":'" + new_page_num + "'";
    strJson += '}';

Try to add dataType and 'contentType' to your code

$.ajax({
        url: 'my url here',
        type: 'post',
        data : JSON.parse(strJson),
        dataType: 'json',
        success: function(data){
            alert(data);
        },
        contentType: "application/json; charset=utf-8"
    });

Also you may want to check official documentation: http://api.jquery.com/jQuery.post/

Upvotes: 0

Yogesh Sawant
Yogesh Sawant

Reputation: 133

In data parameter you are sending a string in json format.

Try sending data in following format.

strJson='name=Will&address=florida';

Let me know if this works.

Upvotes: 0

Jai
Jai

Reputation: 74738

Try to add dataType:"json" too:

$.ajax({
    url: 'my url here',
    type: 'post',
    data: $.parseJSON(strJson),
    dataType: "json",
    success: function(data){
        alert(data);
    }
});

and parse the strJson with $.parseJSON() before sending.

Upvotes: 0

palaѕн
palaѕн

Reputation: 73926

Try this:

$.ajax({
    url: 'my url here',
    type: 'POST',
    dataType: 'json',
    contentType: "application/json; charset=utf-8",
    data: { param: strJson },
    success: function(data){
        alert(data);
    }
});

Upvotes: 0

Related Questions