I'm working on some existing code. I have the following code:
$.ajax({
type: "get",
url: url ,
dataType: "jsonp",
cache: "false",
jsonpCallback: "onJSONPLoad",
success: function(json){
alert('hi!');
//perform operation
},
error: function() {
alert('Error occurs!');
}
});
Now when I'm passing valid url
it works fine. But when I'm passing invalid url
it should through an error alert. But the script is not throwing any error alert. Basically I want validate the url
parameter or it is failing? Please help me to find-out the error in my code or suggest me else way to achieve. I have checked the following links but not able to solve my problem:
jQuery Ajax error handling, show custom exception messages
jQuery ajax error function,
Ajax Success and Error function failure
UPDATE: I have added below code to log the jquery console log.
@Override
public boolean onConsoleMessage(ConsoleMessage cm) {
Log.d("web chrome client", cm.message() + " -- From line "
+ cm.lineNumber() + " of "
+ cm.sourceId() );
return true;
}
Identified the following error:
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
Adding the following code, app is running successfully.
if (Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) {
webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
}
So, basically this is happening as the url is not accessible by the jquery.
Thanks every body for helping me.