×

JS实现ajax请求头添加token

作者:andy0012020.06.29来源:Web前端之家浏览:28937评论:0

JS实现ajax请求头添加token,如何实现呢?直接上DEMO。

function updateType(type) {          
          $.ajax({
                url: mainUrl + "/order/updateType/" + localStorage.orderIds + "/" + type,
                type: "post",
                contentType: "application/json",
                dataType: 'json',
                cache: false,
                async: true,
                beforeSend: function (XMLHttpRequest) {
                    XMLHttpRequest.setRequestHeader("token", localStorage.token);
                },
                success: function (result) {
                    if (result.status === 0) {
                        window.location.href = "my-order.html";
                    } else {
                        alert(result.msg)
                    }
                }
            });
 
 
}


您的支持是我们创作的动力!
温馨提示:本文作者系 ,经Web前端之家编辑修改或补充,转载请注明出处和本文链接:
https://www.jiangweishan.com/article/js20200629a1.html

网友评论文明上网理性发言 已有0人参与

发表评论: