I can not help you with your specific problem, but I can tell you that you will never get a 401 code back. PhoneGap applications have an issue with it: they return code '0' instead of '401' for some reason. It is on their official JIRA:
So whatever you find for solution for your issue, you will just get a new problem.