duanliu6083 2015-01-26 10:58
浏览 63
已采纳

从ajax调用函数时获取“语法错误:意外的令牌”

I'm getting Syntax Errors when calling a php file with ajax.

Uncaught SyntaxError: Unexpected token F
Uncaught SyntaxError: Unexpected token F
Uncaught SyntaxError: Unexpected token F
Uncaught SyntaxError: Unexpected token F
Uncaught SyntaxError: Unexpected token F
Uncaught SyntaxError: Unexpected token F

The change_produkt function is called with an onclick event in a checkbox field. The output of this function is as following:

Return of change_produkt function

Now, I call the second function fill_optionen and pass the arrays to that function. It's doing ajax calls for each object. (6 times in this case)

Javascript:

function fill_optionen(optionen) {
     console.log("fill_optionen called.."); // Debug
     var text = "";

     $j.each(JSON.parse(optionen), function (index, value) {

        jQuery.ajax({
           url: 'include/mutation_helper.php',
           data: {func: "render_opt", option: value},
           type: 'post',
           success: function(output) {
              console.log(JSON.parse(output)); // Debug

           }
        });
     });
  }

function change_produkt() {
     console.log("change_produkt called.."); // Debug

     var id_produkt = $j("#produkt").val();
     console.log("DEBUG -- id_produkt:"+id_produkt);

     jQuery.ajax({
        url: 'include/mutation_helper.php',
        data: {func: "get_opts", produkt: id_produkt},
        type: 'post',
        success: function(output) {
           console.log(JSON.parse(output)); // Debug

           fill_optionen(output);
        }
     });
  }

PHP:

function render_opt() {
    if(!isset($_POST['option'])) {
      echo json_encode("error");
      exit;
    }

    $opt = $_POST['option'];

    $render = render($opt, $_SESSION['mutation']);

    echo json_encode("hello");
}

As soon as I remove the line which calls the render function, it works. But why is there an error? I'm not even printing out that $render variable. (The render function only returns html code in a string.)

  • 写回答

2条回答 默认 最新

  • dow66098 2015-01-26 12:35
    关注

    It seems that you have an PHP error inside your render function. And then PHP prints out its error, which later causes the JavaScript error. Because JSON is excepted as returned output by the ajax request and you got a string back containing the PHP error message. Please look at your browser debugger under the ajax request raw network data, what is really returned.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端