我知道如何将ajax与get/post数据一起使用,如下所示,但我不知道如何将其与令牌密钥一起使用(具有令牌密钥)

$("#read1").click(function () {
        $.support.cors = true;
        $.ajax({
            crossDomain: true,
            url: 'http://localhost:65370/api/travels',
            type: 'GET',
            cache: false,
            error: function (xhr, status, errorThrow) {
            },
            complete: function (xhr) {
            },
            success: function (data) {
            }
        });
    });

  $("#create1").click(function () {
        var person = {
            "travel_id": 4
        };
        $.ajax({
            },
            type: "post",
            url: 'http://localhost:65370/api/travels',
            datatype: "json",
            contenttype: "application/json; charset=utf-8",
            data: person,
            success: function (data) {
            },
            error: function (xhr, status, errorThrow) {
            }
        });
    });

最佳答案

我发现方法如下

    //you just need to put it in Ajax content
    headers:{
                'Authorization': 'Bearer ' + token
            }

当您得到令牌密钥时,可以在“headers”中添加代码。
带标记的完整javascript代码
<script>

    $(document).ready(function()
    {
        var bearer ="";
        var user ={
            grant_type:'password',
            username:'a',
            password:'a'
        };
        $.ajax({
            type: "POST",
            url: "http://localhost:50971/token",
            data:user,
            contentType: "application/x-www-form-urlencoded",
            dataType: "json",
            success: function (data) {
                bearer = JSON.parse(JSON.stringify(data));
                bearer = bearer.access_token;
                Authorization();
            },
            failure: function (response) {
                alert(response.responseText);
            },
            error: function (response) {
                alert(response.responseText);
            }
        });


        function Authorization() {
            $.ajax({
                type: "GET",
                url: "http://localhost:50971/api/employee/GetEmployees",
                headers: {
                    'Authorization': 'Bearer ' + bearer
                },
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (a) {
                    alert(JSON.stringify(a))
                    //$.each(a, function (x, y) {
                    //    alert(x)
                    //    alert(y)
                    //    $.each(y, function (x, y) {
                    //        alert(x)
                    //        alert(y)
                    //    });
                    //});

                    //alert("Hello: " + a.Name + ".\nCurrent Date and Time: " + a.address);
                },
                failure: function (response) {
                    alert(response.responseText);
                },
                error: function (response) {
                    alert(response.responseText);
                }
            });
        }
    })


</script>

09-10 10:38
查看更多