我在my.js中进行错误处理,我正在对其他服务器进行跨域调用,对于动态HTML模板,我正在使用Mustache.js。
$.getJSON(url, function(data, textStatus, xhr) {
$.each(data, function(i, data) {
introPage(data);
});
}).fail(function(Error) {
alert(Error.status);
});
function introPage(data )
{
$.getJSON('myphp.php?jsoncallback=?&template='+testTemplate, function(msg) {
// my operations
}).error(function(data) {
});
}
我们在getJSON中有.fail()来捕获getJSON中的错误。我能够捕获404错误(未找到)但是当遇到406错误(不接受或说无效输入)或401错误(未经授权的访问)时,.fail()似乎无法正常工作
但在控制台中抛出错误
当我点击链接时。它以下面的格式显示jquery回调中的错误
jQuery191014790988666936755_1378113963780({
"error": {
"code": 406,
"message": "Not Acceptable: Sorry, There are no results to display for the given input."
}
});
现在我想知道如何在我的JS中得到这个错误。我没有得到错误代码406和401.I需要该错误代码,以便显示相应的错误页面。
谢谢
答案 0 :(得分:1)
根据jsoncallback=?
参数和响应jQuery191...780({...})
来判断,jQuery似乎正在生成JSONP请求。 JSONP请求不受相同的源策略限制,但它们具有以下限制as described here:
注意:不会为跨域脚本调用此[error]处理程序 跨域JSONP请求。
解决方案: