我正在处理一些现有的代码。我有以下代码:
$.ajax({
        type: "get",
        url: url ,
        dataType: "jsonp",
        cache: "false",
        jsonpCallback: "onJSONPLoad",
        success: function(json){
               alert('hi!');
               //perform operation
        },
        error: function() {
          alert('Error occurs!');
       }
});
现在,当我通过有效时url,它工作正常。但是当我传递无效时url,它应该通过错误警报。但是脚本没有抛出任何错误警报。基本上我想验证url参数还是失败了?请帮助我找出我的代码中的错误或建议我其他方法来实现。我检查了以下链接,但无法解决我的问题:
jQuery Ajax 错误处理,显示自定义异常消息 
jQuery ajax 错误函数,
 Ajax 成功和错误函数失败
更新: 我添加了以下代码来记录 jquery 控制台日志。
  @Override
            public boolean onConsoleMessage(ConsoleMessage cm) {
                Log.d("web chrome client", cm.message() + " -- From line "
                + cm.lineNumber() + " of "
                + cm.sourceId() );
                return true;
            }
识别出以下错误:
XMLHttpRequest cannot load file:///android_asset/xxxx/new_source.json?callback=onJSONPLoad. Origin null is not allowed by Access-Control-Allow-Origin. -- From line 1 of null 
添加以下代码,应用程序运行成功。
if (Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) {
  webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
}
所以,基本上这是发生的,因为 url 不能被 jquery 访问。
 
感谢每个人帮助我。