By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,287 Members | 1,668 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,287 IT Pros & Developers. It's quick & easy.

Xmlhttprequest returns 401 on phonegap/android application

P: 5
I want to access to the service web via my PhoneGap/android application with xmlhttprequest, but the code below returns "Status is 401".
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  2.       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5.       <title>PhoneGap</title>
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7.   <script type="text/javascript" charset="utf-8" src="js/Config/phonegap-0.9.3.js"></script>
  8.  
  9.  
  10.   <link rel="stylesheet" href="css/jquery/jquery.mobile-1.0a1.min.css" />
  11.   <link rel="stylesheet" href="css/Style.css" />
  12.   <script src="js/Config/jquery-1.4.3.min.js"></script>
  13.   <script src="js/Config/jquery.mobile-1.0a1.min.js"></script>
  14.   <script src="jquery.form.js"></script>
  15.      <script src="js/Config/jquery-1.7.1.js"></script>
  16.   <script src="js/Config/jquery-1.7.1.min.js"></script>
  17.  
  18. <script language="javascript">
  19.  
  20. var request = new XMLHttpRequest();
  21.        request.open("GET","http://www.patisserie-orient.fr/prestashop/prestashop/api/products/63",true);
  22.        request.onreadystatechange = function() {
  23. alert("Status is "+request.status);
  24.            if (request.status == 200 || request.status == 0){
  25. response  = request.responseXML.documentElement;
  26. //alert("b");
  27. itemDescription = response.getElementsByTagName('price')[0].firstChild.data;
  28. alert ( itemDescription );
  29. //$("#dropdownlist").append("<hr>" + itemDescription + "<hr>");
  30.            }
  31.        }
  32.        request.send();
  33. </script>
  34. </head>
  35. <body>
  36. <div id="dropdownlist" />
  37. </body>
  38. </html>
  39.  
  40.  
Can anyone help me explaining the error or offering me a solution?
Feb 25 '12 #1

✓ answered by Dormilich

it seems that in Android an XMLHttpRequest does not trigger such a modal window (why should it?). but you can pass username and password in the URI itself.

Share this Question
Share on Google+
8 Replies


Dormilich
Expert Mod 5K+
P: 8,639
401 means that you’re not authorized to use that resource. (list of HTTP status codes)
Feb 26 '12 #2

P: 5
I know that, but how can I resolve the problem?
Feb 26 '12 #3

Dormilich
Expert Mod 5K+
P: 8,639
log in. when I tried to open the URI in the browser, it asked for username/password.
Feb 26 '12 #4

P: 5
yes, I enter them (there is the username and password of my service web), but also req.status=401.
Feb 26 '12 #5

Dormilich
Expert Mod 5K+
P: 8,639
the XMLHttpRequest has to do this verification, too.
Feb 26 '12 #6

P: 5
If I test the code in Internet Explorer, it displays a window to enter user and password then returns 200, but android does not show this window and returns directly 401!!
Feb 26 '12 #7

Dormilich
Expert Mod 5K+
P: 8,639
it seems that in Android an XMLHttpRequest does not trigger such a modal window (why should it?). but you can pass username and password in the URI itself.
Feb 26 '12 #8

P: 5
It resolved :)
Expand|Select|Wrap|Line Numbers
  1. request.open("GET","http://patisserie-orient.fr/prestashop/prestashop/api/products/63?ws_key="+"keywebservice+"&PHP_AUTH_USER="+"password",true);
  2.  
Feb 26 '12 #9

Post your reply

Sign in to post your reply or Sign up for a free account.