简体   繁体   English

如何在HTML表单或A href上设置授权标头

[英]How Set Authorization headers at HTML Form or at A href

I have this code: 我有以下代码:

            $.ajax({
                url: "http://localhost:15797/api/values",
                type: 'get',
                contentType: 'application/json',
                headers: {
                    "Authorization": "Bearer " + token
                }
            })

works fine, but I want to do that without using Ajax, I want something like that: 工作正常,但我想在不使用Ajax的情况下做到这一点,我想要这样的东西:

  <form action="http://localhost:15797/api/values" method="get"> <input type="hidden" name="headers[Authorization]" value="Bearer token" /> <input type="submit" /> </form> 

Is it possible? 可能吗? Or just do something like that without XMLHttpRequest? 或者只是在没有XMLHttpRequest的情况下做类似的事情? How? 怎么样?

You need to send the Authorization argument in the HTTP request header, it's imposed by OAuth flows. 您需要在HTTP请求标头中发送Authorization参数,该参数由OAuth流强加。 Of course you can change it by making changes in OAuth server's code but if you've got no control on the OAuth server's code it's not possible. 当然,您可以通过更改OAuth服务器的代码来更改它,但是如果您无法控制OAuth服务器的代码,则不可能。

So answering your question, no you can't send them with the form's posted data. 因此,回答您的问题,不,您不能将表单的发布数据发送给他们。 However, obviously you can put them in the hidden field and write a JS code to read it from the field and put it in the request header. 但是,显然,您可以将它们放在隐藏字段中,并编写JS代码以从字段中读取它,并将其放在请求标头中。

eg 例如

HTML: HTML:

<input id="tokenField" type="hidden" />
<input id="submitButton" type="button" />

Javascript: Javascript:

$('#submitButton').on('click',function(){
    $.ajax({
          url: "http://localhost:15797/api/values",
          type: 'GET',
          contentType: 'application/json',
          headers: {
                    "Authorization": "Bearer " + $('#tokenField').val()
                 },
          async: false
            }});

Notice the async: false makes your call synchronous, just like a submit. 注意async: false使您的呼叫同步,就像提交一样。 And if you need to post other data to the server you can change the type: 'GET' to type: 'POST' and add another field named data and pass your form data through its value : 如果需要将其他数据发布到服务器,则可以将type: 'GET'更改为type: 'POST'并添加另一个名为data字段,并将表单数据通过其值传递:

<input id="firstName" type="text" />
<input id="lastName" type="text" />
<input id="tokenField" type="hidden" />
<input id="submitButton" type="button" />

$('#submitButton').on('click',function(){
    $.ajax({
          url: "http://localhost:15797/api/values",
          type: 'POST',
          data: { 
                  firstName: $('#firstName').val(),
                  lastName: $('#lastName').val()
                },
          contentType: 'application/json',
          headers: {
                    "Authorization": "Bearer " + $('#tokenField').val()
                 },
          async: false
            }});

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

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