admin管理员组

文章数量:1431720

I am trying to load an XML file that asks for a username and password.

The code I'm using is as follows that I am trying to send the username and password in the URL:

xmlhttp.open("POST","http://admin:[email protected]/myfile.xml",false);

The full code of my page looks like this:

<html>
<body>

<textarea id="test1" name="test1" cols="90" rows="30">
XML file will be displayed here
</textarea><br>

<script type="text/javascript">

if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }


  xmlhttp.onreadystatechange=function()
   {
   if (xmlhttp.readyState==4 && xmlhttp.status==200)
     {
     document.getElementById("test1").value=xmlhttp.responseText;
     } else
     {
     alert('Panel not municating.Reason: '+xmlhttp.status);
     }
   }

xmlhttp.open("POST","http://admin:[email protected]/myfile.xml",false);
xmlhttp.send();

</script>
</body>
</html>

Anyone know what I am doing wrong?

I am trying to load an XML file that asks for a username and password.

The code I'm using is as follows that I am trying to send the username and password in the URL:

xmlhttp.open("POST","http://admin:[email protected]/myfile.xml",false);

The full code of my page looks like this:

<html>
<body>

<textarea id="test1" name="test1" cols="90" rows="30">
XML file will be displayed here
</textarea><br>

<script type="text/javascript">

if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }


  xmlhttp.onreadystatechange=function()
   {
   if (xmlhttp.readyState==4 && xmlhttp.status==200)
     {
     document.getElementById("test1").value=xmlhttp.responseText;
     } else
     {
     alert('Panel not municating.Reason: '+xmlhttp.status);
     }
   }

xmlhttp.open("POST","http://admin:[email protected]/myfile.xml",false);
xmlhttp.send();

</script>
</body>
</html>

Anyone know what I am doing wrong?

Share Improve this question edited Jul 2, 2012 at 5:50 Jari 2,15213 silver badges20 bronze badges asked Jul 2, 2012 at 5:48 AaronAaron 3,68913 gold badges36 silver badges49 bronze badges 5
  • what error are you getting? are you sure that the remote server accepts HTTP Basic Auth? – Jim Deville Commented Jul 2, 2012 at 5:51
  • So what happens when you type the URL into a browser window? – Michael Commented Jul 2, 2012 at 5:51
  • I don't get any error messages it just sits there, but if I remove the username and password from the URL code it will ask for a username and password which I can manually enter in and it works fine, but I want to do it automatically. any ideas? – Aaron Commented Jul 2, 2012 at 5:53
  • i think you need to pass them in a header instead of the url – dbrin Commented Jul 2, 2012 at 5:54
  • what do you mean by that?? I am not sure what you mean by that – Aaron Commented Jul 2, 2012 at 5:57
Add a ment  | 

3 Answers 3

Reset to default 1

First, add this function to create the authorization header contents:

function make_base_auth(user, password)
{
  var tok = user + ':' + pass;
  var hash = Base64.encode(tok);
  return "Basic " + hash;
}

Then, call the function and store the return value in a separate variable:

var auth = make_basic_auth('admin', 'admin');

Lastly, pass the value of auth to the XMLHttpRequest object:

xmlhttp.setRequestHeader('Authorization', auth);
xmlhttp.send();

Attribution: http://coderseye./2007/how-to-do-http-basic-auth-in-ajax.html

I ended up getting it working by doing the following:

Change:

xmlhttp.open("POST","http://admin:[email protected]/myfile.xml",false); 

To:

xmlhttp.open("POST","http://admin:[email protected]/myfile.xml",false,"username","password");

See this question for a similar scenario. How to use Basic Auth with jQuery and AJAX? Note one answer states that IE does not support credentials in a URL (as you are attempting to do).

本文标签: javascriptUsername and Password in URLStack Overflow