axios 不跨域发请求携带 X-Requested-With

提示: 不携带 X-Requested-With 请求头,通过 accept请求头,也可以判断是否 ajax请求,详见:判断是否ajax请求原理

说明

不跨域 携带 X-Requested-With 头,只需要设置 axios 即可

注意:如果 跨域,后端需要做额外处理

关键代码

axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

例子

axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
axios.post('http://localhost:8080/RegCtrl', 
  this.form
)
.then(function (res) {
  console.log(res);
})
.catch(function (error) {// 请求失败处理
  console.log(error);
});

原文出处:/show_1IX72IDoeDfs.html