Sending JSON jQuery Ajax to PHP and back

后端 未结 4 1959
借酒劲吻你
借酒劲吻你 2021-01-27 12:46

I\'m having problems sending a JSON jQuery array via Ajax to a PHP script. What is the problem here:

var tee = $(\'#voting_image img\').attr(\'id\');
var vote =          


        
相关标签:
4条回答
  • 2021-01-27 13:05

    AFAIK, there is no $.toJSON method in jQuery, you are probably looking for $.parseJSON and by the way you are already creating JSON here:

    var thing = {tee: tee, vote: vote};
    
    0 讨论(0)
  • 2021-01-27 13:29

    I think the problem is that you send data as object, try to send as array var thing = {tee: tee, vote: vote}; to array

    0 讨论(0)
  • 2021-01-27 13:29

    Thanks for your responces, I went with:

    $.getJSON(
                '/vote_save.php?vote='+encoded,
                function(data) 
                {
                    $('#voting_hint_name').html(data.bob);
                    $('#voting_buttons').html('<div id="voting_buttons"><a href="#" id="vote_yes">PRINT IT</a><a href="#" id="vote_no">DON\'T PRINT IT</a></div>');
                }   
        );
    

    instead of $.ajax and it worked.

    0 讨论(0)
  • 2021-01-27 13:30

    Check out this question: Serializing to JSON in jQuery

    The accepted answer links to a JSON serialization plug-in recommended by John Resig (the creator of jQuery). It doesn't really address your specific bug, but perhaps using that plug-in will help you arrive at a stable solution.

    From looking at it briefly, if you use that plug-in, it appears you would then replace this line:

    var encoded = $.toJSON(thing);
    

    with this:

    var encoded = JSON.stringify(thing); 
    

    Hope that helps!

    0 讨论(0)
提交回复
热议问题