日常开发记录-axios.defaults.headers['X-Request-With'] = 'XMLHttpRequest'

本文介绍了X-Requested-With头字段的作用,特别是在区分AJAX请求与普通请求方面。当该字段值为'XMLHttpRequest'时,表明请求是由JavaScript发起的异步请求;若值为空,则通常为表单提交等常规请求方式。
// X-Requested-With: XMLHttpRequest是给服务器用的,用于区别 AJAX 请求(异步)还是普通(同步)的请求(一般指表单提交)的
// x-requested-with 赋予 ‘XMLHttpRequest’ 值表示这是一个ajax请求,而如果值为null的话 表示一个普通的请求,服务器用来检测是否为异步 
// 如果服务器没做任何针对的反馈那么都一样。

 转载:http://t.csdn.cn/wKkGD

HotelDetailView.vue:114 GET http://localhost:8080/api/hotels/1/room-types 403 (Forbidden) dispatchXhrRequest @ xhr.js:195 xhr @ xhr.js:15 dispatchRequest @ dispatchRequest.js:51 Promise.then _request @ Axios.js:163 request @ Axios.js:40 Axios.<computed> @ Axios.js:213 wrap @ bind.js:5 fetchHotelDetails @ HotelDetailView.vue:114 (匿名) @ runtime-core.esm-bundler.js:2836 callWithErrorHandling @ runtime-core.esm-bundler.js:199 callWithAsyncErrorHandling @ runtime-core.esm-bundler.js:206 hook.__weh.hook.__weh @ runtime-core.esm-bundler.js:2816 flushPostFlushCbs @ runtime-core.esm-bundler.js:385 flushJobs @ runtime-core.esm-bundler.js:427 Promise.then queueFlush @ runtime-core.esm-bundler.js:322 queueJob @ runtime-core.esm-bundler.js:317 effect2.scheduler @ runtime-core.esm-bundler.js:5480 trigger @ reactivity.esm-bundler.js:265 endBatch @ reactivity.esm-bundler.js:323 notify @ reactivity.esm-bundler.js:614 trigger @ reactivity.esm-bundler.js:588 set value @ reactivity.esm-bundler.js:1465 finalizeNavigation @ vue-router.mjs:3503 (匿名) @ vue-router.mjs:3368 Promise.then pushWithRedirect @ vue-router.mjs:3335 push @ vue-router.mjs:3260 navigate @ vue-router.mjs:2315 callWithErrorHandling @ runtime-core.esm-bundler.js:199 callWithAsyncErrorHandling @ runtime-core.esm-bundler.js:206 invoker @ runtime-dom.esm-bundler.js:729 api.js:46 Forbidden access - you do not have permission to access this resource. (匿名) @ api.js:46 Promise.then _request @ Axios.js:163 request @ Axios.js:40 Axios.<computed> @ Axios.js:213 wrap @ bind.js:5 fetchHotelDetails @ HotelDetailView.vue:114 (匿名) @ runtime-core.esm-bundler.js:2836 callWithErrorHandling @ runtime-core.esm-bundler.js:199 callWithAsyncErrorHandling @ runtime-core.esm-bundler.js:206 hook.__weh.hook.__weh @ runtime-core.esm-bundler.js:2816 flushPostFlushCbs @ runtime-core.esm-bundler.js:385 flushJobs @ runtime-core.esm-bundler.js:427 Promise.then queueFlush @ runtime-core.esm-bundler.js:322 queueJob @ runtime-core.esm-bundler.js:317 effect2.scheduler @ runtime-core.esm-bundler.js:5480 trigger @ reactivity.esm-bundler.js:265 endBatch @ reactivity.esm-bundler.js:323 notify @ reactivity.esm-bundler.js:614 trigger @ reactivity.esm-bundler.js:588 set value @ reactivity.esm-bundler.js:1465 finalizeNavigation @ vue-router.mjs:3503 (匿名) @ vue-router.mjs:3368 Promise.then pushWithRedirect @ vue-router.mjs:3335 push @ vue-router.mjs:3260 navigate @ vue-router.mjs:2315 callWithErrorHandling @ runtime-core.esm-bundler.js:199 callWithAsyncErrorHandling @ runtime-core.esm-bundler.js:206 invoker @ runtime-dom.esm-bundler.js:729 HotelDetailView.vue:122 Failed to fetch hotel details or room types: AxiosError {message: 'Request failed with status code 403', name: 'AxiosError', code: 'ERR_BAD_REQUEST', config: {…}, request: XMLHttpRequest, …} fetchHotelDetails @ HotelDetailView.vue:122 await in fetchHotelDetails (匿名) @ runtime-core.esm-bundler.js:2836 callWithErrorHandling @ runtime-core.esm-bundler.js:199 callWithAsyncErrorHandling @ runtime-core.esm-bundler.js:206 hook.__weh.hook.__weh @ runtime-core.esm-bundler.js:2816 flushPostFlushCbs @ runtime-core.esm-bundler.js:385 flushJobs @ runtime-core.esm-bundler.js:427 Promise.then queueFlush @ runtime-core.esm-bundler.js:322 queueJob @ runtime-core.esm-bundler.js:317 effect2.scheduler @ runtime-core.esm-bundler.js:5480 trigger @ reactivity.esm-bundler.js:265 endBatch @ reactivity.esm-bundler.js:323 notify @ reactivity.esm-bundler.js:614 trigger @ reactivity.esm-bundler.js:588 set value @ reactivity.esm-bundler.js:1465 finalizeNavigation @ vue-router.mjs:3503 (匿名) @ vue-router.mjs:3368 Promise.then pushWithRedirect @ vue-router.mjs:3335 push @ vue-router.mjs:3260 navigate @ vue-router.mjs:2315 callWithErrorHandling @ runtime-core.esm-bundler.js:199 callWithAsyncErrorHandling @ runtime-core.esm-bundler.js:206 invoker @ runtime-dom.esm-bundler.js:729 authStore.js:49 POST http://localhost:8080/api/users/login 403 (Forbidden) dispatchXhrRequest @ xhr.js:195 xhr @ xhr.js:15 dispatchRequest @ dispatchRequest.js:51 Promise.then _request @ Axios.js:163 request @ Axios.js:40 httpMethod @ Axios.js:226 wrap @ bind.js:5 login @ authStore.js:49 wrappedAction @ pinia.mjs:1399 store.<computed> @ pinia.mjs:932 handleLogin @ Auth.vue:219 await in handleLogin cache.<computed>.cache.<computed> @ runtime-dom.esm-bundler.js:1713 callWithErrorHandling @ runtime-core.esm-bundler.js:199 callWithAsyncErrorHandling @ runtime-core.esm-bundler.js:206 invoker @ runtime-dom.esm-bundler.js:729 api.js:46 Forbidden access - you do not have permission to access this resource. (匿名) @ api.js:46 Promise.then _request @ Axios.js:163 request @ Axios.js:40 httpMethod @ Axios.js:226 wrap @ bind.js:5 login @ authStore.js:49 wrappedAction @ pinia.mjs:1399 store.<computed> @ pinia.mjs:932 handleLogin @ Auth.vue:219 await in handleLogin cache.<computed>.cache.<computed> @ runtime-dom.esm-bundler.js:1713 callWithErrorHandling @ runtime-core.esm-bundler.js:199 callWithAsyncErrorHandling @ runtime-core.esm-bundler.js:206 invoker @ runtime-dom.esm-bundler.js:729 settle.js:19 Uncaught (in promise) AxiosError {message: 'Request failed with status code 403', name: 'AxiosError', code: 'ERR_BAD_REQUEST', config: {…}, request: XMLHttpRequest, …}
06-24
import axios from 'axios'; import qs from 'qs' // import da from "element-ui/src/locale/lang/da"; // import manage from './manage.js' // axios.defaults.baseURL = manage.apiHost; // import { getToken,clear } from '@/utils/auth' axios.defaults.timeout = 60 * 1000 * 10; // axios.defaults.cache = false // console.log("当前环境",process.env.NODE_ENV); // /*第一层if判断生产环境和开发环境*/ // if (process.env.NODE_ENV === 'production') { // /*第二层if,根据.env文件中的VUE_APP_FLAG判断是生产环境还是测试环境*/ // if (process.env.VUE_APP_FLAG === 'pro') { // //production 生产环境 // axios.defaults.baseURL = 'http://127.0.0.1:8080'; // } else { // //test 测试环境 // axios.defaults.baseURL = 'http://127.0.0.1:8080'; // } // } else { // //dev 开发环境 // // axios.defaults.baseURL = 'http://127.0.0.1:8080'; // } // // let baseUrl = axios.defaults.baseURL; // // console.log("当前host:", baseUrl); axios.interceptors.request.use( config => { // config.headers['X-Requested-With'] = 'XMLHttpRequest' // config.headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'; // config.headers['Content-Type'] = 'application/json; charset=UTF-8'; // config.data = qs.stringify(manage.data) // config.headers['Content-Type'] = 'application/json; charset=UTF-8'; // config.headers['Authorization'] = getToken() ? getToken() : null; // manage.headers['X-CSRFToken'] = sessionStorage.getItem('csrftoken'); // config.headers['Cookie'] = 'Hm_lvt_b0668f30d62e1597bdb36d05edea8960=1604221499,1604560772,1604655241; Hm_lpvt_b0668f30d62e1597bdb36d05edea8960=1604911501; sid=f2295ce4-5905-47d2-b119-9747e7749674'; return config; }, error => { return Promise.reject(error); } ); axios.interceptors.response.use( response => { return response; }, error => { return Promise.reject(error); } ); function checkStatus(response) { if (response.status === 200 || response.status === 304) { return response['data']; } else { throw new Error(response.data.message); // eslint-disable-line } throw new Error(response.statusText); // eslint-disable-line } export function request(config) { return axios.request(config).then(checkStatus); } export function get(url, params) { // let queryData = Object.assign(params, {t: new Date().getTime()}) return axios.get(url, {params}).then(checkStatus); } export function delet(url, params) { return axios.delete(url, params).then(checkStatus); } export function post(url, data, config) { return axios.post(url, qs.stringify(data), config).then(checkStatus); } export function postDefine(url, data, config) { return axios.post(url, data, config).then(checkStatus); } export function postData(url, data, config) { return axios.post(url, data, config).then(checkStatus); } export function put(url, data, config) { return axios.put(url, data, config).then(checkStatus); } export function patch(url, data, config) { return axios.patch(url, data, config).then(checkStatus); }
最新发布
12-09
runtime-core.esm-bundler.js:41 [Vue warn]: Missing required prop: "index" at <ElSubMenu> at <BaseTransition mode="out-in" onBeforeEnter=fn<onBeforeEnter> onEnter=fn ... > at <Transition mode="out-in" onBeforeEnter=fn<onBeforeEnter> onEnter=fn<onEnter> ... > at <ElMenuCollapseTransition> at <ElMenu active-text-color="#ffd04b" background-color="#232323" text-color="#fff" ... > at <ElAside width="200px" > at <ElContainer class="layout-container" > at <Layout onVnodeUnmounted=fn<onVnodeUnmounted> ref=Ref< undefined > > at <RouterView> at <App> warn2 @ runtime-core.esm-bundler.js:41 api/user/userInfo:1 Failed to load resource: the server responded with a status of 401 (Unauthorized) api/category:1 Failed to load resource: the server responded with a status of 401 (Unauthorized) api/article?pageNum=1&pageSize=3:1 Failed to load resource: the server responded with a status of 401 (Unauthorized) settle.js:19 Uncaught (in promise) AxiosError settle.js:19 Uncaught (in promise) AxiosError settle.js:19 Uncaught (in promise) AxiosError user.js:11 POST http://localhost:5173/api/user/register 500 (Internal Server Error) dispatchXhrRequest @ xhr.js:256 xhr @ xhr.js:49 dispatchRequest @ dispatchRequest.js:51 Promise.then request @ Axios.js:122 httpMethod @ Axios.js:185 wrap @ bind.js:5 userRegisterService @ user.js:11 register @ Login.vue:44 callWithErrorHandling @ runtime-core.esm-bundler.js:158 callWithAsyncErrorHandling @ runtime-core.esm-bundler.js:166 emit @ runtime-core.esm-bundler.js:669 (匿名) @ runtime-core.esm-bundler.js:7479 handleClick @ use-button.ts:57 callWithErrorHandling @ runtime-core.esm-bundler.js:158 callWithAsyncErrorHandling @ runtime-core.esm-bundler.js:166 invoker @ runtime-dom.esm-bundler.js:595 use-button.ts:57 [Vue warn]: Unhandled error during execution of component event handler at <ElButton class="button" type="primary" auto-insert-space="" ... > at <ElFormItem> at <ElForm key=0 ref="form" size="large" ... > at <ElCol span=6 offset=3 class="form" > at <ElRow class="login-page" > at <Login onVnodeUnmounted=fn<onVnodeUnmounted> ref=Ref< Proxy(Object) {__v_skip: true} > > at <RouterView> at <App> warn2 @ runtime-core.esm-bundler.js:41 logError @ runtime-core.esm-bundler.js:216 handleError @ runtime-core.esm-bundler.js:208 (匿名) @ runtime-core.esm-bundler.js:169 Promise.catch callWithAsyncErrorHandling @ runtime-core.esm-bundler.js:168 emit @ runtime-core.esm-bundler.js:669 (匿名) @ runtime-core.esm-bundler.js:7479 handleClick @ use-button.ts:57 callWithErrorHandling @ runtime-core.esm-bundler.js:158 callWithAsyncErrorHandling @ runtime-core.esm-bundler.js:166 invoker @ runtime-dom.esm-bundler.js:595 settle.js:19 Uncaught (in promise) AxiosError {message: 'Request failed with status code 500', name: 'AxiosError', code: 'ERR_BAD_RESPONSE', config: {…}, request: XMLHttpRequest, …} settle @ settle.js:19 onloadend @ xhr.js:112 XMLHttpRequest.send dispatchXhrRequest @ xhr.js:256 xhr @ xhr.js:49 dispatchRequest @ dispatchRequest.js:51 Promise.then request @ Axios.js:122 httpMethod @ Axios.js:185 wrap @ bind.js:5 userRegisterService @ user.js:11 register @ Login.vue:44 callWithErrorHandling @ runtime-core.esm-bundler.js:158 callWithAsyncErrorHandling @ runtime-core.esm-bundler.js:166 emit @ runtime-core.esm-bundler.js:669 (匿名) @ runtime-core.esm-bundler.js:7479 handleClick @ use-button.ts:57 callWithErrorHandling @ runtime-core.esm-bundler.js:158 callWithAsyncErrorHandling @ runtime-core.esm-bundler.js:166 invoker @ runtime-dom.esm-bundler.js:595
11-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值