【发布时间】:2016-06-30 03:56:20
【问题描述】:
我正在编写一个 AJAX 函数,但看起来有错误。我似乎找不到任何东西。请帮忙!我浏览了这些问题:this、this 和 this,但没有一个有效。这是我的代码:
function registerDevice(fp,uid)
{
$.ajax({
type: "POST",
async: true,
url: "backend.php",
data: {"fp": fp, "uid" : uid},
success: function(output) {
var json = eval('(' + output + ')');
var status = json['status'];
if(status == "success")
{
//redirect user.
setTimeout(function(){
//do what you need here
<?php
if(isset($_GET['goto']))
{
echo 'window.location.replace("'.$_GET["goto"].'");';
}
else
{
echo 'window.location.replace("index.php");';
}
?>
}, 2000);
}
else
{
$("#login-msg").text("Something went wrong. Please try again.");
$("#login-alert").fadeTo(4000, 500).slideUp(500, function(){
$("#login-alert").hide();
});
}
},
error: function(error) {
$("#login-msg").text("There was an unknown error.");
$("#login-alert").fadeTo(4000, 500).slideUp(500, function(){
$("#login-alert").hide();
});
}
});
}
它是这样调用的:
$('body').on("click", "#mybtn", function (e) {
registerDevice(result,uid);
});
这是我在 Google Chrome 中遇到的错误:
VM411:1 Uncaught SyntaxError: Unexpected token )
success @ login_alpha.php:344
j @ jquery.js:3148
fireWith @ jquery.js:3260
x @ jquery.js:9314
b @ jquery.js:9718
P.S:第 344 行指的是这一行:
var json = eval('(' + output + ')');
编辑 1:backend.php
<?php
$uid = $_POST['uid'];
$fp = $_POST['fp'];
//my code here.
$response_array['status'] = "success";
$response_array['type'] = "cookie";
?>
【问题讨论】:
-
试试这个方法
var json = eval(+'(' + output + ')'+); -
@guradio 不,错误仍然存在。这一次从头开始。上次它仅在调用该函数时才出现! :(
-
output的内容是什么?可以直接将输出作为json消费吗? -
@kazenorin 等等,我会用更多的细节来编辑这个问题。
-
你没有解析输出尝试先解析它你正在返回一个json
标签: javascript jquery ajax google-chrome error-handling