SyntaxError: expected expression, got '.' in using $.getJSON() function
SyntaxError: expected expression, got '.' in using $.getJSON() function
我有一个外部 JS 文件,它使用 $.getJSON
从 JSON 加载数据
(function() {
var url = "dummy.json";
$.getJSON( url,
.done(function( data ) {
console.log(data);
})
});
每当我加载与此脚本相关的 html 页面时,我都会在控制台中不断收到此错误
SyntaxError: expected expression, got '.' .done(function( data ) {
我的问题是 - 我的问题的原因是什么?任何帮助,将不胜感激。
这是一个语法错误。您应该根据请求链接 .done()
。
(function() {
var url = "dummy.json";
$.getJSON( url, function(){
console.log('done something');
}).done(function( data ) {
console.log(data);
}).fail(function(){
console.log('something went wrong');
});
});
根据文档,通常它需要一个默认的回调方法,我想如下:
var jqxhr = $.getJSON( "example.json", function() {
console.log( "success" );
})
.done(function() {
console.log( "second success" );
})
.fail(function() {
console.log( "error" );
})
.always(function() {
console.log( "complete" );
});
我有一个外部 JS 文件,它使用 $.getJSON
(function() {
var url = "dummy.json";
$.getJSON( url,
.done(function( data ) {
console.log(data);
})
});
每当我加载与此脚本相关的 html 页面时,我都会在控制台中不断收到此错误
SyntaxError: expected expression, got '.' .done(function( data ) {
我的问题是 - 我的问题的原因是什么?任何帮助,将不胜感激。
这是一个语法错误。您应该根据请求链接 .done()
。
(function() {
var url = "dummy.json";
$.getJSON( url, function(){
console.log('done something');
}).done(function( data ) {
console.log(data);
}).fail(function(){
console.log('something went wrong');
});
});
根据文档,通常它需要一个默认的回调方法,我想如下:
var jqxhr = $.getJSON( "example.json", function() {
console.log( "success" );
})
.done(function() {
console.log( "second success" );
})
.fail(function() {
console.log( "error" );
})
.always(function() {
console.log( "complete" );
});