簡體   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