【问题标题】:Uncaught SyntaxError: Unexpected token (, no error visibleUncaught SyntaxError: Unexpected token (, no error visible
【发布时间】:2016-06-30 03:56:20
【问题描述】:

我正在编写一个 AJAX 函数,但看起来有错误。我似乎找不到任何东西。请帮忙!我浏览了这些问题:thisthisthis,但没有一个有效。这是我的代码:

      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


【解决方案1】:

eval 函数需要一个字符串来评估。您正在尝试评估我认为的 JSON 对象。

试试:

var json = eval('(' + JSON.stringify(output) + ')');

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval

【讨论】:

  • 那为什么还要麻烦eval-ing呢?由于JSON.stringify 只会将有效的 JSON 字符串化
  • eval 将运行脚本。
  • 好吧,我已经解决了这个问题。我注释了 json_encode 行,这就是输出不是 JSON 格式的原因!好吧,那里有愚蠢的错误!
【解决方案2】:

我找到了解决办法,很傻。

backend.php 没有给出 JSON 响应! 这是原始代码:

<?php
$uid = $_POST['uid'];
$fp = $_POST['fp'];

//my code here.
$response_array['status'] = "success";
$response_array['type'] = "cookie";

?>

我刚刚添加了这一行:

echo json_encode($response_array);

这确保我向 AJAX 调用的成功函数发回了 JSON 响应!多么愚蠢的错误。

backend.php 的最终编辑:

<?php
$uid = $_POST['uid'];
$fp = $_POST['fp'];

//my code here.
$response_array['status'] = "success";
$response_array['type'] = "cookie";

//print json response
echo json_encode($response_array);
?>

【讨论】:

    猜你喜欢
    • 2017-12-18
    • 1970-01-01
    • 2012-05-17
    • 2019-02-10
    • 2014-07-08
    • 2011-03-09
    • 2014-01-06
    • 2012-04-10
    相关资源
    最近更新 更多