我想在客户端的每个请求上添加HTTP标头。无法找出方法。
试图将以下脚本添加到HTML中以设置标头,例如:
<script>
res.setHeader('Authorization', "I am JWT Bearer token");
</script>
答案 0 :(得分:0)
您只能在从AJax或客户端的其他API请求模块进行API调用时添加标头,而不能通过提交<form>
并向<form>
添加操作来添加标头
通过Ajax打电话,您可以执行以下操作:
<script>
function submitForm() {
var data = {};//Your JSON Data
//You can make this a global variable to use everywhere.
//Or you can make a function what will be responsible to make all aJax calls.
//Function will receive 3 arguments: json data, success-callback and error-callback, or in place of call back you can use Promise.
var headers = {
"Contect-Type": "application/json",
"Authorization": "Basic : your auth"
};
$.ajax({
url: 'http://google.com',
type: 'POST',//'GET','PUT','DELETE'
headers: headers,
data: data,
success: function(result) {
alert("success");
},
error: function (error) {
alert('error ',error);
}
});
}
</script>
您需要调用此函数来代替
<form action="submitAPI" method="POST"><input type="submit" value="Submit" /></form>
使用此:
<form><input onclick="submitForm()" value="Submit" /></form>