[英]AngularJS - Stuck Handling response after $resource.save (expecting json)
Hello first of all thanks for your support,您好首先感谢您的支持,
I getting started with angular and I am trying to use conmsume data from an API for my app.我开始使用 angular,我正在尝试将来自 API 的数据用于我的应用程序。 I am having a few problems with this.
我在这方面遇到了一些问题。 First of all CORS:
首先是CORS:
To run local http server I am using the one that comes with node.js (using http-server command).要运行本地 http 服务器,我使用的是 node.js 附带的服务器(使用 http-server 命令)。 I am using http://www.mocky.io/ to test the app.
我正在使用http://www.mocky.io/来测试该应用程序。 I've generated differents (with headers I've found around the net that are supposed to fix it) response there to try to fix CORS (always getting preflight error) but nothing seems to work.
我已经生成了不同的(带有我在网络上发现的应该修复它的标题)响应以尝试修复 CORS(总是出现预检错误),但似乎没有任何效果。 I have added this to my save method (inside a factory):
我已将此添加到我的保存方法中(在工厂内):
save: {
method: 'POST',
headers: {
'Access-Control-Allow-Origin': '*'
}
}
If I use a Chrome extension called CORS I can bypass that and receive response but then I am not able to manage the promise and get the data inside the response.如果我使用名为 CORS 的 Chrome 扩展程序,我可以绕过它并接收响应,但随后我无法管理承诺并获取响应中的数据。 I would like to be able to show the response's json on the view.
我希望能够在视图上显示响应的 json。
$scope.submitForm = function() {
var promise = null;
promise = CheckFactory.save($scope.partner).$promise;
$scope.result = promise.data;
}
This functions sends the data from the form to the factory and perform the request but then I am lost and do not know how to manage the data I need from the response.此函数将数据从表单发送到工厂并执行请求,但随后我迷路了,不知道如何从响应中管理我需要的数据。
Thanks in advance :)提前致谢:)
Basically you need to put .then
function over your save
method call promise.基本上你需要把
.then
函数放在你的save
方法调用承诺上。 So that will call .then
function's once data save request gets completed.因此,一旦数据保存请求完成,就会调用
.then
函数。
$scope.submitForm = function() {
CheckFactory.save($scope.partner).$promise
//it will called success callback when save promise resolved.
.then(function(data){ //success
$scope.result = data;
}, function(error){ //error
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.