谷歌浏览器:语法错误:输入意外结束

Google Chrome: SyntaxError: Unexpected end of input

本文关键字:意外 结束 输入 错误 语法 谷歌浏览器      更新时间:2023-09-26

我一直在铬上收到此错误,但在 Firefox 上却没有,它让我发疯,因为尽管现在搜索了几个小时,我还是找不到解决方案。我基本上从服务器获取JSON,然后想将其插入DOM中。这是我的代码...

function lookup(inputString){
    if(inputString.length == 0){        //hide suggestions
        $('#suggestions').empty()
                 .fadeOut();
         }
    else{           //make an AJAX call
         $.ajax({
            type: 'GET',
            url: '{% url "search.views.search" inputString="xyz" %}'.replace("xyz", inputString.toString()),
            dataType: 'json',
                success: function(search_results){
            suggestions = JSON.parse(JSON.stringify(search_results));
                        alert(suggestions[0].name);
                    }
                })
            }
         }
最后

你需要一个;

$.ajax({
            type: 'GET',
            url: '{% url "search.views.search" inputString="xyz" %}'.replace("xyz", inputString.toString()),
            dataType: 'json',
                success: function(search_results){
            suggestions = JSON.parse(JSON.stringify(search_results));
                        alert(suggestions[0].name);
                    }
                });

同样的结论:)

您错过了分号;

JSLINT可以帮助您找到此类错误: http://www.jslint.com/

function lookup(inputString) {
  if (inputString.length === 0) {
    $('#suggestions').empty().fadeOut();
  }
  else {
    $.ajax({
      type: 'GET',
      url: '{% url "search.views.search" inputString="xyz" %}'.replace("xyz", inputString.toString()),
      dataType: 'json',
      success: function(search_results) {
        suggestions = JSON.parse(JSON.stringify(search_results));
        alert(suggestions[0].name);
      }
    });
  }
}