I have an issue with Angular $resource. I use it with our API server with CORS enabled.
我对Angular $资源有一个问题。我在启用了CORS的API服务器上使用它。
No matter what error code server returns (tried 500, 422, 409) resource always calls success callback.
无论服务器返回什么错误代码(尝试过500,422,409),资源总是调用成功回调。
Action in the controller:
控制器中的动作:
@SpacePostsService.save { spaceId: @space._id }, @post, @_createSuccess, (errors)=>¬
@_createError(errors, form)
Where SpacePostsService:
SpacePostsService = $resource postsUrl, {},¬
query:¬
isArray: true¬
method: 'GET'¬
responseType: 'json'¬
transformResponse: (data, headers)->¬
results = data.results || data¬
results = [results] unless _.isArray(results)¬
results
Does anyone have an idea what I am doing wrong?
有谁知道我做错了什么?
PS. For readability purposes I didn't show all the code, SpacePostService is a factory and it's properly injected in to the controller.
PS。出于可读性目的,我没有显示所有代码,SpacePostService是一个工厂,它已正确地注入控制器。
1 个解决方案
#1
2
Finally I've found the issue. It turns out I had global interceptor defined in other place:
最后我发现了这个问题。事实证明我在其他地方定义了全局拦截器:
AjaxInProgress =
request: (config)->
$rootScope.$broadcast 'ajax:in-progress'
config
response: (response)->
$rootScope.$broadcast 'ajax:finished'
response
responseError: (response)->
$rootScope.$broadcast 'ajax:finished'
return response
The last line in responseError
method is the most important one - because I didn't reject the promise so Angular thought I recovered from error and it called success callback in the $resource function. I changed last line in the AjaxInProgress to:
responseError方法中的最后一行是最重要的一行 - 因为我没有拒绝承诺,所以Angular认为我从错误中恢复了,它在$ resource函数中调用了成功回调。我将AjaxInProgress中的最后一行更改为:
responseError: (response)->
$rootScope.$broadcast 'ajax:finished'
$q.reject(response)
It solved the problem.
它解决了这个问题。
Thank you guys for your help.
谢谢你们的帮助。
#1
2
Finally I've found the issue. It turns out I had global interceptor defined in other place:
最后我发现了这个问题。事实证明我在其他地方定义了全局拦截器:
AjaxInProgress =
request: (config)->
$rootScope.$broadcast 'ajax:in-progress'
config
response: (response)->
$rootScope.$broadcast 'ajax:finished'
response
responseError: (response)->
$rootScope.$broadcast 'ajax:finished'
return response
The last line in responseError
method is the most important one - because I didn't reject the promise so Angular thought I recovered from error and it called success callback in the $resource function. I changed last line in the AjaxInProgress to:
responseError方法中的最后一行是最重要的一行 - 因为我没有拒绝承诺,所以Angular认为我从错误中恢复了,它在$ resource函数中调用了成功回调。我将AjaxInProgress中的最后一行更改为:
responseError: (response)->
$rootScope.$broadcast 'ajax:finished'
$q.reject(response)
It solved the problem.
它解决了这个问题。
Thank you guys for your help.
谢谢你们的帮助。