我的POST方法效果很好。 但我的方法PUT不起作用。我找不到我的错误..
我在浏览器中有OPTION ...
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
答案 0 :(得分:1)
我承认我没有阅读您的所有代码,但我首先看到的是您在应用程序控制器中不允许使用CORS for PUT。你允许GET,POST,OPTIONS。你应该添加PUT。
我不会在应用程序控制器中配置它,而是在application.rb中配置
config.action.dispatch.default_headers = {
'Access-Control-Allow-Origin' => '*'
'Access-Control-Request-Method' => %w{GET PUT POST OPTIONS}.join(",")
}
也许您想查看:https://github.com/cyu/rack-cors
最后但并非最不重要的是,如果你发布你的routes.rb
会很棒