我正在尝试从ajax调用获取变量:error,armount_payout,this.result1,this.result2,this.result3,这是我到目前为止的代码,但它返回错误
Uncaught SyntaxError: Unexpected token < jquery.js:25_
.parseJSON jquery.js:25
Game.restart slots.js:188
(anonymous function) slots.js:127
_.event.dispatch jquery.js:25
q.handle jquery.js:24
这是我的PHP代码:
$arr = array(
0=>$payout_amount,
1=>$error,
2 => $num1win,
3=> $num2win,
4=> $num3win
);
echo json_encode($arr);
和我的jquery代码:
data=$.ajax({
type: "POST",
url: "../php/runslotsgame.php",
data: "address=" + address,
dataType: 'json',
async: false
}).responseText;
var dataArray = jQuery.parseJSON(data);
error = dataArray [0];
armount_payout = dataArray [1];
this.result1=dataArray [2];
this.result2=dataArray [3];
this.result3=dataArray [4];
console.log("yay");
“yay”也不在我的控制台中
有人看到我在这里做错了吗?
答案 0 :(得分:0)
不要忘记在ajax响应中添加Content-Type = application / json标头:
Content-Type = application/json
另外,不确定您正在使用哪个版本的jQuery,但是您需要检查回调,因为.ajax()是一个异步函数。
$.post(url,{address:address},function(data){
var dataArray = data;
//where you need to use your data array
});
答案 1 :(得分:0)
直接使用浏览器点击您的JSON制作网址并检查输出。您的错误消息表明JSON无效。
您可以使用JSON validator检查问题。