API调用:未捕获SyntaxError:意外的标记:
我正在运行ajax调用来从API获取一些数据。 通话本身工作正常,我得到200(OK)作为回应。 此外,我可以在我的开发工具中查看JSON数据。 但最后警报是“错误”,我的控制台中有一个“Uncaught SyntaxError:Unexpected token:”。
那里出了什么问题?
$.ajax({
url: 'my url',
type:'GET',
xhrFields: {
withCredentials: true
},
cache: false,
dataType: 'jsonp',
contentType: 'application/x-www-form-urlencoded',
// headers: {
// 'Authorization': 'Basic ' + btoa('uberall:cat-aspirin-window-cake')
// },
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', 'Basic ' + btoa(user + ":" + pw));
},
success: function(text) { alert('success'); },
error: function (text) { alert('error'); },
complete: function(text) { alert('complete'); }
});
如果你使用jsonp,你应该实现一个回调方法,看起来你错过了它。
您可能首先尝试通过将以下函数替换为错误字段来打印该错误,查看它是否缺少jsonpCallback或其他错误
error:function(jqXHR, textStatus, errorThrown){
console.log("jqXHR",jqXHR);
console.log("textStatus",textStatus);
console.log("errorThrown",errorThrown);
}
如果它是jsonp你应该检查服务器端看看应该是什么回调,但你不知道,你可以尝试添加尽可能多的服务器设置为默认
注意:这不是一个好的实践
jsonp: 'callback'
也参考JSONP NOT CALL
链接地址: http://www.djcxy.com/p/46023.html上一篇: API Call: Uncaught SyntaxError: Unexpected token :
下一篇: "Uncaught SyntaxError: Unexpected token :" with $http.jsonp