jQuery 如何在 JavaScript 中的 HTTP 标头中传递 X-Auth-Token

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19928168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 00:49:58  来源:igfitidea点击:

How to pass X-Auth-Token in the HTTP header in JavaScript

javascriptjquerytokenxauth

提问by GibboK

I have a website which uses Token-based authentication. At first, username and password are sent to log the user in and receive the token. For subsequent calls, I need to include the token as X-Auth-Tokenin HTTP header.

我有一个使用基于令牌的身份验证的网站。首先,发送用户名和密码以登录用户并接收令牌。对于后续调用,我需要X-Auth-Token在 HTTP 标头中包含令牌。

I would like to know how to do that, in vanilla JavaScript or using jQuery. Could you please provide me a sample of code?

我想知道如何在 vanilla JavaScript 或使用 jQuery 中做到这一点。你能给我提供一个代码示例吗?

回答by asiviero

In jQuery it would be something like this:

在 jQuery 中,它会是这样的:

$.ajax({
   url : myurl,
   headers: {
        'X-Auth-Token' : token
   });

More details on what you can do with $.ajax()are in the docs

有关您可以做什么的更多详细信息,请$.ajax()参阅文档