weixin_33743880 2014-08-24 07:56 采纳率: 0%
浏览 24

angularJS + rails 4 /放置方法

my POST method works well. but my method PUT does not work. I can not find my error..

I have OPTION in the browser...

f {$promise: Object, $resolved: false, $get: function, $save: function, $query: function…}
 app.js:20
f {id: 1, title: "title updated", content: "Corporis distinctio praesentium est sunt. Est expedita quod. Possimus praesentium est quisquam.", user_id: 0, category_id: 5…}
 app.js:18
OPTIONS http://195.154.114.102:3000/posts/1.json 404 (Not Found) angular.js:8553
XMLHttpRequest cannot load http://195.154.114.102:3000/posts/1.json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.  




Remote Address:195.154.114.102:3000
Request URL:http://195.154.114.102:3000/posts.json
Request Method:OPTIONS
Status Code:404 Not Found
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
Access-Control-Request-Headers:accept, content-type
Access-Control-Request-Method:PUT
Cache-Control:max-age=0
Connection:keep-alive
Host:195.154.114.102:3000
Origin:null
User-Agent:Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36
Response Headersview source
Connection:Keep-Alive
Content-Length:16692
Content-Type:text/html; charset=utf-8
Date:Sun, 24 Aug 2014 07:15:35 GMT
Server:WEBrick/1.3.1 (Ruby/2.1.2/2014-05-08)
X-Request-Id:e46b4495-a712-478f-9390-f3db95efd9fe
X-Runtime:0.106774

app.js

var app = angular.module('MonApp', ['ngResource']);

app.controller('PostsCtrl', function($scope, $resource){

    var Post = $resource('http://195.154.114.102:3000/posts/:id.json', null, {
        'update': { method: 'PUT', params: {id: "@id"}}
    });

    $scope.posts = Post.query();
    $scope.post = Post.get({id: 1}, function() {
        $scope.post.title = "title updated";
        $scope.post.$update();
        console.log($scope.post);
    });

        console.log($scope.post);



    /*
        var post = new Post;
        post.title = "second article title";
        post.content = "second article content";
        post.$save();
    */

});

cat app/controllers/application_controller.rb

skip_before_filter :protect_from_forgery
before_filter :set_acces

def set_acces
        headers['Access-Control-Allow-Origin'] = '*'
        headers['Access-Control-Request-Method'] = %w{GET POST OPTIONS}.join(",")
end

cat app/controllers/application_controller.rb (gem ok)

config.middleware.use Rack::Cors do
  allow do
    origins '*'
    resource '*', :headers => :any, :methods => [:get, :post, :options]
  end
end

cat app/controllers/posts_controller.rb

  # PATCH/PUT /posts/1
  # PATCH/PUT /posts/1.json
  def update
    respond_to do |format|
      if @post.update(post_params)
        format.html { redirect_to @post, notice: 'Post was successfully updated.' }
        format.json { render :show, status: :ok, location: @post }
      else
        format.html { render :edit }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end
  • 写回答

1条回答 默认 最新

  • weixin_33713503 2014-08-24 10:02
    关注

    I admit I did not read all of your code but what I saw first is that you don't allow CORS for PUT in your application controller. You allow GET, POST, OPTIONS. You should add PUT.

    I would not configure it in the application controller but rather in application.rb like

    config.action.dispatch.default_headers = { 
     'Access-Control-Allow-Origin' => '*'
     'Access-Control-Request-Method' => %w{GET PUT POST OPTIONS}.join(",")
    }
    

    Maybe you want to check out: https://github.com/cyu/rack-cors

    Last but not least it would be great if you could post your routes.rb

    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程