我无法发送通过Ajax的JSON jQuery的数组到一个PHP脚本的问题。什么是这里的问题:
VAR发球= $('#voting_image IMG)ATTR('身份证')。
变种表决= 1;
VAR的事情= {发球:发球,投票:投票};
变种EN codeD = $ .toJSON(事);
$阿贾克斯({
网址:/vote_save.php,
键入:POST,
数据类型:JSON,
数据:'票='+ EN codeD,
成功:函数(数据)
{
VAR回到= $ .evalJSON(数据)。名称;
$('#voting_hint_name)HTML(回);
$('#voting_buttons)HTML('< DIV ID =voting_buttons>< A HREF =#ID =vote_yes>打印< / A>< A HREF =# ID =vote_no> DON \'T打印< / A>< / DIV>');
},
错误:函数()
{
$('#voting_buttons)HTML('< DIV ID =voting_buttons>< A HREF =#ID =vote_yes>打印< / A>< A HREF =# ID =vote_no> DON \'T打印< / A>< / DIV>');
警报(有一个问题,您的投票未保存,请重试!);
}
});
这是PHP
如果(使用isset($ _ POST ['投票'])及和放大器;使用isset($ _ SESSION ['用户']))
{
$ tee_data = json_de code($ _ POST ['投票']);
$ the_tee = $ tee_data ['三通'];
$的反响=阵列(名称=>亚历克斯哇','测试'=>'1');
回声json_en code($性反应);
}
其他 {
回声错误;
}
我收到的萤火虫的错误是:
错误:JSON.parse
解决方案感谢您的responces,我就跟着:
$。的getJSON(
/vote_save.php?vote='+en$c$cd,
功能(数据)
{
$('#voting_hint_name)HTML(data.bob);
$('#voting_buttons)HTML('< DIV ID =voting_buttons>< A HREF =#ID =vote_yes>打印< / A>< A HREF =# ID =vote_no> DON \'T打印< / A>< / DIV>');
}
);
而不是$就和它的工作。
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 = 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
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:
Error: JSON.parse
解决方案
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.