带有某些标头的 xhr GET 不起作用?
我有一个 javascript 函数,它接受两个值作为参数,我从 html 发送这些值(用户名和令牌值(例如:34378463782 )。我正在尝试使用 GET 方法进行休息调用,并且必须将令牌作为请求标头发送我需要知道的是,代码有什么问题吗?
是代码:
function getUser(user,token)
{
var xmlhttp;
var text,x,i;
var url="http://abc.xyz.com:8890/uauth/" +user;
alert("url :"+url);
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
if (xmlhttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET",url, true);
alert("Outside the Main Code");
xmlhttp.onreadystatechange=function()
{
alert("Above the Main Code if block");
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
alert("Inside the Main Code");
xmlDoc=xmlhttp.responseXML;
text="";
x=xmlDoc.getElementsByTagName("response");
for (i=0;i<x.length;i++)
{
text=text + x[i].childNodes[0].nodeValue + "<br />";
}
document.getElementById("myNewDiv").innerHTML=text;
}
};
xmlhttp.setRequestHeader("xyz-Authorization: ", token);
xmlhttp.send(null);
}
以下
评论。
欢迎
I have a javascript function which takes two values as parameter which I'm sending from html(username and token value(say:34378463782 ). I'm trying to make a rest call using GET method and have to send token as a requestheader with the call. All I need to know, is there anything wrong with code?
Here is the code:
function getUser(user,token)
{
var xmlhttp;
var text,x,i;
var url="http://abc.xyz.com:8890/uauth/" +user;
alert("url :"+url);
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
if (xmlhttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET",url, true);
alert("Outside the Main Code");
xmlhttp.onreadystatechange=function()
{
alert("Above the Main Code if block");
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
alert("Inside the Main Code");
xmlDoc=xmlhttp.responseXML;
text="";
x=xmlDoc.getElementsByTagName("response");
for (i=0;i<x.length;i++)
{
text=text + x[i].childNodes[0].nodeValue + "<br />";
}
document.getElementById("myNewDiv").innerHTML=text;
}
};
xmlhttp.setRequestHeader("xyz-Authorization: ", token);
xmlhttp.send(null);
}
Pls no suggestions for jquery use.
Comments welcome.
Thanks for the help in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议使用 jquery 进行 xhr-ing。更清楚了。
I suggest to use jquery to xhr-ing. It is more clear.