繁体   English   中英

从Angular / IOnic App到Rails API的CRUD

[英]CRUD from Angular/IOnic App to rails API

有人可以帮我解决下一个话题吗? 我在Rails中有一个API,可以选择执行完整的CRUD 我已经使用POSTMAN测试了API,并且效果很好。 但是现在我尝试使用它,并通过Ionic和Angular创建的移动应用程序使用它,并且只有GET选项起作用。 我已经安装并配置了gem rack-cors。 而且我还在控制器中使用这部分代码进行测试。 我得到的错误如下。 非常感谢您的帮助,如果我做错了事,我是一名学生,请原谅。

配置/ application.rb中

     config.middleware.insert_before 0, "Rack::Cors" do
        allow do
            origins '*'
            resource '*',
            headers: :any, 
            methods: [:get, :post, :options, :delete, :put]
        end
    end

ANGULAR / IONIC APP中的控制器

     Define the event resource, adding an update method
     var Event = $resource('http://localhost:3000/api/v1/books/:id', {id:'@id'},
     { 
         update: 
         {
            method: 'PUT'
         }
    });

    // Use get method to get the specific object by ID
    // If object found, update. Else throw error
    Event.get({'id': 1}).$promise.then(function(res) {
       // Success (object was found)

       // Set event equal to the response
       var e = res;

       // Pass in the information that needs to be updated
       e.title = "2000";

       // Update the resource using the custom method we created
       Event.update(e)

    }, function(errResponse) {
       // Failure, throw error (object not found)
       alert('event not found');
   });

这是错误:

XMLHttpRequest无法加载http:// localhost:3000 / api / v1 / books / 1 对预检请求的响应未通过访问控制检查:请求的资源上不存在“ Access-Control-Allow-Origin”标头。 因此,不允许访问源' http:// localhost:5000 '。 响应的HTTP状态码为404。

如果您使用的是Angular 6和Ionic。 CLI支持代理配置,代理配置将代理有助于开发的后端API。

https://github.com/angular/angular-cli/blob/master/docs/documentation/stories/proxy.md

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM