【发布时间】:2018-01-16 17:24:52
【问题描述】:
大家早上好!
我一直在通过 Google Chrome 开发者控制台测试我的 JavaScript 代码,并修复了所有错误,除了一个说,“Uncaught SyntaxError: Unexpected token :” 我发现了类似的问题Stack Overflow,但我的情况不同。此外,通常当控制台列出错误时,它会提供一个查看错误的链接,该链接会带有红色下划线。但是,这一次控制台列出了错误,但没有用红色下划线标记代码的错误部分。
对我来说,所有内容的格式都正确,我所有的双冒号看起来都正确放置。你能帮忙吗?提前致谢!
这是我的 JavaScript 代码:
$(document).ready(function(){
//when search is clicked run code
$(search).on('Click'(function(){
//gets search input
var searchTerm= $("#searchTerm").val();
//API url with searchTerm
var url="https://en.wikipedia.org/wiki/Special:ApiSandbox#action=query&titles=Main%20Page&prop=revisions&rvprop=content&format=jsonfm";
//create an AJAX call within .on("Click") function
$.ajax({})
//We intend to do a GET call (because we are retrieving data from the server)
type: "GET"
url: url,
//code will be paused while other code waiting for this to finish
Async: false,
dataType: json,
Success: function(data){
//How will you test for success of json call function
console.log()data;
},
//Do something when its a function and do something else when it's a failure
Error: function(errorMessage){
//Console out a warning that says something like "error" alert
alert("Error");
}
}); //search
});//document ready
【问题讨论】:
-
错误中的行号将帮助您以后调试它。
-
是的,但如果您以正确的方式操作,您也可以使用 on。它有两个问题。大小写很重要,并且缺少逗号。另一个也很明显。任何好的短绒都会捡起这些。看例子 on.... api.jquery.com/on
-
解析器在看到错误时停止。查看我发布到
on的 jQuery 文档的链接,并将其与您所拥有的进行比较。 -
并且 Ajax 行是错误的......只是注意到......正确缩进你的代码,你可以发现这些问题。使用类似 jshint jshint.com 或 eslint、jslint 等的 linter。
-
@codebwoy 基本上你真的应该学会使用你的工具来帮助你发现你的问题。您的代码没有正确缩进没有帮助。将 linter 集成到您的 IDE 中,以便有助于实施良好实践。
标签: javascript jquery syntax-error unexpected-token