将JSON jQuery Ajax发送到PHP并返回

时间:2022-10-17 13:10:41

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

我在通过Ajax将JSON jQuery数组发送到PHP脚本时遇到问题。这里有什么问题:

var tee = $('#voting_image img').attr('id');
var vote = 1;
var thing = {tee: tee, vote: vote};
var encoded = $.toJSON(thing);

$.ajax({
    url:             '/vote_save.php',
    type:            'POST',
    dataType:        'json',
    data:            'vote='+encoded,
    success: function(data)
    {
        var back = $.evalJSON(data).name;
        $('#voting_hint_name').html(back);
        $('#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>');
    },
    error:function ()
    {
        $('#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>');
        alert("There was a problem, your vote was not saved, please try again!");
    }
});

This is the PHP

这是PHP

if (isset($_POST['vote'])&&isset($_SESSION['user']))
{
    $tee_data = json_decode($_POST['vote']);
    $the_tee = $tee_data['tee'];
    $responce = array('name'=> 'Alex Wow', 'test'=> '1');
    echo json_encode($responce);
}
else {
    echo "error";
}

The error I am getting in Firebug is:

我在Firebug中遇到的错误是:

Error: JSON.parse

错误:JSON.parse

4 个解决方案

#1


0  

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

AFAIK,jQuery中没有$ .toJSON方法,你可能正在寻找$ .parseJSON,并且你已经在这里创建了JSON:

var thing = {tee: tee, vote: vote};

#2


0  

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

我认为问题是你发送数据作为对象,尝试发送为数组var thing = {tee:tee,vote:vote};到数组

#3


0  

Check out this question: Serializing to JSON in jQuery

看看这个问题:在jQuery中序列化为JSON

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.

接受的答案链接到John Resig(jQuery的创建者)推荐的JSON序列化插件。它并没有真正解决您的具体错误,但也许使用该插件将帮助您获得稳定的解决方案。

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!

希望有所帮助!

#4


0  

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.

而不是$ .ajax,它工作。

#1


0  

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

AFAIK,jQuery中没有$ .toJSON方法,你可能正在寻找$ .parseJSON,并且你已经在这里创建了JSON:

var thing = {tee: tee, vote: vote};

#2


0  

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

我认为问题是你发送数据作为对象,尝试发送为数组var thing = {tee:tee,vote:vote};到数组

#3


0  

Check out this question: Serializing to JSON in jQuery

看看这个问题:在jQuery中序列化为JSON

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.

接受的答案链接到John Resig(jQuery的创建者)推荐的JSON序列化插件。它并没有真正解决您的具体错误,但也许使用该插件将帮助您获得稳定的解决方案。

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!

希望有所帮助!

#4


0  

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.

而不是$ .ajax,它工作。