3

1.一些信息

我有一个 AngularJS 1.3.15 客户端应用程序向我的环回 api 发出请求。

在应用程序中,我拦截请求结果(使用 $httpProvider.interceptors)并解释状态代码以在用户执行某项操作时显示有意义的消息 - 例如,我拦截 401 响应告诉用户他没有授权,200 表示当他成功保存东西等等。

2.问题

例如,如果用户尝试上传太大的图像,请求有效负载将超过我的远程限制,我能够拦截 requestError 但状态代码始终显示 0。查看浏览器的控制台,我看到了这一点:

PUT http://localhost:3000/api/Modules?id=9 413 (Request Entity Too Large)
XMLHttpRequest cannot load http://localhost:3000/api/Modules?id=9. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. The response had HTTP status code 413.

问题是如果服务器离线,我在拦截requestError时也会得到状态码0,所以我无法区分连接服务器的错误和发送太多数据的错误。

快递路由器中间件有没有可能出现问题?好像找不到遇到同样问题的人。。。

4

1 回答 1

0

我有同样的问题,但问题不是由 AngularJS 产生的。问题提示是:

 No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.

由于没有为引发 413 错误的组件配置 CORS,AngularJS 无法获得正确的状态码。在启动 nodejs -> express -> bodyparser 并且问题消失之前,我必须在我的后端启用 cors。

于 2017-07-22T18:08:11.487 回答