简体   繁体   English

如何传递XMLHttpRequest请求的标头

[英]How to pass headers for an XMLHttpRequest request

I have an api which requires few headers to passed to get the response.Below is the working postman 我有一个api,需要传递几个标头才能获得响应。以下是工作邮递员

在此处输入图片说明

Now Iam doing an http call to consume this and below is the code Iam using 现在我正在做一个http调用来消耗这个,下面是我正在使用的代码

 var jobendpoint = 'http://rundeck:4440/api/14/project/Demo/jobs';
 var http = new XMLHttpRequest();
 http.open('GET',jobendpoint,false);
    http.setRequestHeader('Accept','application/json');
    http.setRequestHeader('Content-type','application/json');
    http.setRequestHeader('X-Rundeck-Auth-Token','XgTeIxQLUiv3lOSl8cXNkt4bxIGFjbzl');
 http.send();
  var joblist = http.responseText;
  http.onreadystatechange = function() {//Call a function when the state changes.
        if(http.readyState === XMLHttpRequest.DONE && http.status == 200) {
        joblist = http.responseText;
         }
        }
        return joblist
}

But when I try this it is not working.I can see that headers are not properly passed.How can we pass those headers to this GET call.can someone help . 但是当我尝试它不起作用时。我可以看到标题没有正确传递。我们如何将这些标题传递给此GET调用。有人可以帮忙吗?

Updating error****** Im getting 403 forbiddden error since my headers are not properly set.Below is the screen shot of headers passed in network tab of firefox 更新错误******由于我的标头设置不正确,因此我收到403禁止错误。以下是在firefox的“网络”标签中传递的标头的屏幕截图 在此处输入图片说明

Ive edited the headers directly in firefox n/w tab and sent the call and it worked.Below is the screen shot 我已经直接在Firefox的n / w选项卡中编辑了标题,并发送了呼叫并成功了。

在此处输入图片说明

How can I add the headers the way I had in second screen shot 如何添加第二个屏幕截图中的标题

The MDN page for XMLHttpRequest.setRequestHeader() states: XMLHttpRequest.setRequestHeader()的MDN页面指出:

For your custom fields, you may encounter "not allowed by Access-Control-Allow-Headers in preflight response" exception when you send request to cross domain. 对于您的自定义字段,当您向跨域发送请求时,可能会遇到“飞行前响应中Access-Control-Allow-Headers不允许的”异常。 In this situation, you need set " Access-Control-Allow-Headers " in your response header at server side. 在这种情况下,您需要在服务器端的响应头中设置“ Access-Control-Allow-Headers ”。

In your case, the X-Rundeck-Auth-Token header is such a custom header, and therefore this header must be listed in the Access-Control-Allow-Headers response header from the server, otherwise you'll get an error as above and the request will fail. 在您的情况下, X-Rundeck-Auth-Token标头就是这样的自定义标头,因此该标头必须在服务器的Access-Control-Allow-Headers响应标头中列出,否则您将收到如上所述的错误并且请求将失败。

Accept , Accept-Language , Content-Type , Content-Length are whitelisted headers that can be set without specifying them in the Access-Control-Allow-Headers header; AcceptAccept-LanguageContent-TypeContent-Length是列入白名单的标头,可以设置这些标头,而无需在Access-Control-Allow-Headers标头中指定它们; there's also a list of request headers that can never be set, even if they are listed. 还有一个永远无法设置的请求标头列表 ,即使它们已列出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM