ajaxt weird error: Uncaught SyntaxError: Unexpected token if
ajaxt weird error: Uncaught SyntaxError: Unexpected token if
我有这个javascript:
function updatePage() {
if (request.readyState == 4)
if (request.status == 200)
var data = JSON.parse(request.responseText);
update_select($('select[name=island_name]'), data);
else if (request.status == 404)
alert("Request url does not exist");
else
alert("Error: status code is " + request.status);
}
但是我一直得到一个Uncaught SyntaxError: Unexpected token if
错误,它停止了我的脚本。什么好主意吗?
您丢失了一堆大括号,并且在update_select()
行上,这导致了一个问题。对于大多数这些块来说,大括号并不是严格要求的,但是因为您试图在第一个if()
中执行两条语句,所以您将需要它们。最好把它们放在任何地方。
function updatePage() {
if (request.readyState == 4) {
if (request.status == 200) {
var data = JSON.parse(request.responseText);
// Second statement executed after the if()
// MUST use {} on this block
update_select($('select[name=island_name]'), data);
}
else if (request.status == 404) {
alert("Request url does not exist");
}
else {
alert("Error: status code is " + request.status);
}
}
}
function updatePage() {
if (request.readyState == 4)
if (request.status == 200)
{
var data = JSON.parse(request.responseText);
update_select($('select[name=island_name]'), data);
}
else if (request.status == 404)
alert("Request url does not exist");
else
alert("Error: status code is " + request.status);
}
您缩进了第5行(update_select...
),但它实际上不在if
中。
您在第2行和第3行缺少大括号{:
function updatePage() {
if (request.readyState == 4) {
if (request.status == 200) {
var data = JSON.parse(request.responseText);
update_select($('select[name=island_name]'), data);
}
else if (request.status == 404)
alert("Request url does not exist");
else
alert("Error: status code is " + request.status);
}
}
相关文章:
- ReactJS Unexpected Token ) on Safari only
- Ajax错误:SyntaxError: Unexpected token <错误
- ajaxt weird error: Uncaught SyntaxError: Unexpected token if
- index.php:13 Uncaught SyntaxError: Unexpected token: Ci中的错误消
- 接收错误"Uncaught SyntaxError: Unexpected token "用Shin
- React JS应用程序- Uncaught SyntaxError: Unexpected token <
- nodeJS syntaxError: unexpected token this
- 尝试使用标记的模板字符串给出'Uncaught SyntaxError: Unexpected token
- 未捕获的SyntaxError: Unexpected token <在JQuery UI地图插件?在本地工作,而不是在
- Javascript for循环返回“Uncaught SyntaxError: Unexpected Token <&
- 在尝试动态删除表行时,得到Uncaught SyntaxError: Unexpected token}
- 未捕获的SyntaxError: Unexpected token <谷歌分析谷歌标签管理器
- 为什么json返回"Unexpected Token:"当使用$.ajax()时,我的json格式正
- 查找&;Unexpected token;&;的原因/行Express-JS中的错误
- 内联onclick事件返回"Uncaught SyntaxError: Unexpected token (&
- 未捕获的SyntaxError: Unexpected token <在Javascript中调用外部web服务时出现错
- 来自UglifyJs的错误:SyntaxError: Unexpected token: operator (>)
- 未捕获的SyntaxError: Unexpected token ILLEGAL (PHP &Javascript)
- 未捕获的SyntaxError: Unexpected token: Can't resolve
- Uncaught SyntaxError: Unexpected token)在解析JSON字符串时