简体   繁体   English

AngularJS - $resource.save 后卡住处理响应(期望 json)

[英]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.

相关问题 将多个对象传递给angularjs $ resource.save() - Passing multiple objects to angularjs $resource.save() 使用AngularJS的ngResource中带回调的resource.save()和$ promise之间的区别 - Difference between resource.save() with callback and $promise in ngResource using AngularJS 如何在angularjs中将多个对象传递给$ resource.save - How to pass in multiple objects to $resource.save in angularjs 角$ resource.save到json文件不起作用 - Angular $resource.save to json file does not work AngularJS-处理RESTful $ resource响应 - AngularJS - handling RESTful $resource response $ resource.save使用“资源”而不是简单的ID进行响应 - $resource.save responds with a “Resource” instead of simple ID AngularJS资源-如何从。$ save获取响应 - Angularjs Resource - how to get the response from .$save Vue.js:vue-resource 使用 path 参数调用 resource.save() - Vue.js: vue-resource calling resource.save() with path parameter 使用角度$ resource.save进行保存会导致视图重绘/重排(由于集合观察者) - Saving with angular $resource.save causes view to redraw/reflow due to collection watcher AngularJS:$ resource $ save后无法获取responseHeaders - AngularJS: Unable to get responseHeaders after a $resource $save
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM