【发布时间】:2018-06-07 06:10:51
【问题描述】:
我有以下代码:
var qs = require('qs');
const ROOT_URL = 'http://localhost:56765/';
const data = qs.stringify({ username, password, grant_type: 'password' });
axios.post(`${ROOT_URL}token`, data)
.then(response => {
debugger;
dispatch(authUser());
localStorage.setItem('token', response.data.token);
})
.catch((error) => {
debugger;
dispatch(authError(error.response));
});
当我运行这段代码时,我点击了调试器,错误对象在 catch 块中有 Error: Network Error at createError (http://localhost:3000/static/js/bundle.js:2188:15) at XMLHttpRequest.handleError (http://localhost:3000/static/js/bundle.js:1724:14)。但是,在 Chrome 的网络选项卡中,状态代码为 200,但是当我单击此请求的响应/预览选项卡时,没有数据。如果我点击继续,我实际上会按预期在响应/预览选项卡中获取令牌和其他数据,但此时它已经到达了 catch 块,因此不会命中 then 块。
我什至调试了后端并且它没有发回错误,所以我认为这是一个前端错误。有谁知道是什么原因造成的?
编辑:
只是为了添加更多详细信息,如果我将请求更改为使用 fetch 而不是 axios,我会收到另一个错误 TypeError: Failed to fetch。用于调用的代码是:
fetch(`${ROOT_URL}token`, {
method: 'POST',
body: data
})
【问题讨论】:
-
你使用
static服务器吗?错误 URL'http://localhost:3000/static/js'与您的操作 URL'http://localhost:56765/token'不同。这只是假设,所以我可能错了 -
什么是静态服务器?我的服务器是默认的 C# WebAPI
-
我认为这与github issue about an axios bug有关,也许您可以尝试一些解决方法。
-
@Dyo 只花了几个小时尝试上述线程中的所有内容。他们都没有工作;我仍然遇到同样的错误。感谢您尝试
-
由于这似乎是 web api 令牌请求,您应该启用 https。当 https 被禁用时,你会得到一个静默失败,邮递员没有响应。
标签: c# reactjs asp.net-web-api fetch axios