问题
I have upgraded my project to Phonegap 1.4 to Cordova 1.8.1. Ajax call to webservice was working fine with 401 Unauthorized , when I am passing invalid username / password in Phonegap 1.4.
After up gradation it is not working , I am not getting any response and web service stay there.
If change code to Phonegap 1.4 , it starts working.
Please help
回答1:
I've had this problem for the last couple months and finally figured it out.
First of all, the temporary workaround that we used was to implement a timeout period on the request. That worked, but the response was always a 404. We didn't know if it actually failed authentication, or if our services were down. Not an acceptable workaround for our system.
Finally, after MUCH googling, I found the actual problem.
When a 401 gets returned, (which is a Basic Authentication, authentication failure), it adds a header to the response, called "WWW-Authenticate". This tells the browser to prompt the user to try and login. iOS webview doesn't know what to do with that, so that's why the application hangs.
From your service, all you need to do is remove the WWW-Authenticate header, and you should start to see the 401's in your error function.
回答2:
I just had the same problem. If you pass incorrect credentials and use
$.ajax({
...
timeout: 5000, // Some timeout value that makes sense
...
});
then the error callback is called with {"readyState":0,"status":0,"statusText":"timeout"}
. In that case you would have to guess that the real error is the HTTP 401.
Alternatively you can use
$.ajax({
...
async: false, // :-(
...
});
and your error callback will get something like {"readyState":4,"responseText":"<html>...</html>","status":401,"statusText":"Unauthorized"}
back.
来源:https://stackoverflow.com/questions/12060619/not-getting-401-unauthorised-with-cordova-jquery-ajax-call-upto-phonegap-1-4-i