1

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???

4
  • also (not the problem), it should be done, not success. api.jquery.com/deferred.done Commented Mar 28, 2013 at 8:41
  • post the error in console log Commented Mar 28, 2013 at 8:42
  • 1
    you should escape the i too: strJson += "'"+ i + "':'" + v + "',"; Commented Mar 28, 2013 at 8:58
  • 1
    and data has to be a JSON object, not a JSON string: data : JSON.parse(strJson), Commented Mar 28, 2013 at 9:02

5 Answers 5

1

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);
    }
});
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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.

Comments

0

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.

Comments

0

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/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.