ajax 和 axios 添加请求头

1、ajax 添加请求头

$.ajax({
            url: url,
            type:"post",
            dataType:"json",
            data:data,
            beforeSend: function (XMLHttpRequest) {
                XMLHttpRequest.setRequestHeader("authorization", "authorization");
                XMLHttpRequest.setRequestHeader("timestamp", new Date().getTime());
            },
            success:function (data) {
            },
            error:function (data) {
            }
        })

2、axios 添加请求头

 this.$axios.post(url, data,
        {
          headers: {
            'authorization': 'authorization',
            'timestamp': new Date()
          }
        })
        .then(res => {
        })
service.interceptors.request.use(
  config => {
    config.headers.common['timestamp'] = times;
	config.headers.common['authorization'] = 'authorization'
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

你可能感兴趣的:(ajax,前端,javascript)