Ajax 302状态axios的响应未定义
我将axios与typescript一起使用 我有一个简单的axios拦截器,代码如下Ajax 302状态axios的响应未定义,ajax,typescript,axios,identityserver4,Ajax,Typescript,Axios,Identityserver4,我将axios与typescript一起使用 我有一个简单的axios拦截器,代码如下 // Add a response interceptor axios.interceptors.response.use((response: AxiosResponse<any>) => { // Do something with response data return response; }, (error: any) => { // Do something w
// Add a response interceptor
axios.interceptors.response.use((response: AxiosResponse<any>) => {
// Do something with response data
return response;
}, (error: any) => {
// Do something with response error
// Here "error.response" is undefined.
return Promise.reject(error);
});
我试图做的是,我需要重定向到其他位置标识服务器SSO页面,但我猜这与未经身份验证的情况无关
因此,当调用我的API时,如果未使用正确的位置进行身份验证,则返回302状态代码
但axios并没有自动重定向到该位置
如果我必须手动重定向,这仍然可以
但我得到了错误。响应未定义
那么现在我如何重定向?由于错误,我无法检测到状态代码。响应为未定义
在“网络”选项卡中,它显示如下
“响应”选项卡显示以下内容。没有可用的响应数据
我做错了什么?我想下面是这个问题的答案。我还是不确定 所以不要把标记作为答案 Ajax无法处理302个请求 它是由浏览器处理的
正如我所理解的,302代码是由浏览器处理的。回答302后,浏览器将接收位置标题传递的页面,并将其传递给Axios。因此,您可以在response.data中找到页面html。同样在response.request.responseURL中,您可以找到需要重定向到的URL 所以我做了这样的东西:
axios.post('.', Data)
.then(response => {
if (response.status === 200) {
window.location.href = response.request.responseURL;
}
})
.catch(error => {console.log(error)});
它现在对我很有用。如果你控制台.logerror?Error:createError.js的网络错误?XMLHttpRequest.handleError xhr.js的2d83:16?b50d:87So console.direrror,打开属性并尝试查找状态我这样做了,它不工作。