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
3 Answers
Reset to default 1First, 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
版权声明:本文标题:javascript - Username and Password in URL - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745581482a2664647.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论