$(function () {
    $.ajax({
        type: 'GET',
        url: '/api',
        data: {},
        dataType: 'json',
        beforeSend:function(xhr){
          xhr.setRequestHeader("token","Basic "+"生成token值");
        },
        success:function (data) {
            console.log(data)
            if (data.err == 0) {
                alert("成功");
            }else {
                alert("失败")
            }
        }

    })
})
$(function () {
    $.ajax({
        type: 'GET',
        url: '/api',
        data: {},
        dataType: 'json',
        headers: {"token","Basic "+"生成token值"}
        success:function (data) {
            console.log(data)
            if (data.err == 0) {
                alert("成功");
            }else {
                alert("失败")
            }
        }

    })
})

 

 

更多推荐

ajax请求中自定义添加请求头token两种方式