如何在HTML表单或A href上设置授权标头 [英] How Set Authorization headers at HTML Form or at A href

查看:1013
本文介绍了如何在HTML表单或A href上设置授权标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此代码:

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

工作正常,但是我想在不使用Ajax的情况下做到这一点,我想要这样的东西:

works fine, but I want to do that without using Ajax, I want something like that:

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

有可能吗?或者只是在没有XMLHttpRequest的情况下做类似的事情?怎么样?

Is it possible? Or just do something like that without XMLHttpRequest? How?

推荐答案

您需要在HTTP请求标头中发送Authorization参数,该参数由OAuth流强加.当然,您可以通过更改OAuth服务器的代码来进行更改,但是如果您无法控制OAuth服务器的代码,则不可能.

You need to send the Authorization argument in the HTTP request header, it's imposed by OAuth flows. 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.

因此,回答您的问题,不,您无法将其与表单的已发布数据一起发送.但是,显然,您可以将它们放在隐藏字段中,并编写JS代码以从该字段中读取它,并将其放在请求标头中.

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.

例如

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
            }});

请注意,async: false使您的呼叫同步,就像提交一样.如果需要将其他数据发布到服务器,则可以将type: 'GET'更改为type: 'POST'并添加另一个名为data的字段,并将表单数据通过其值传递:

Notice the async: false makes your call synchronous, just like a submit. 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 :

<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
            }});

这篇关于如何在HTML表单或A href上设置授权标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆