我正在尝试在客户端创建一个带有主干的应用程序,在服务器端创建一个Rails。它有效但我在创建,更新和销毁方面存在问题。
我这样做:
m = new MyFirstProjectServer.Models.Inquiry({id: 1})
m.fetch() #work fine
m.set({title: 'title', description: 'description'})
m.save() #don't work
ajax调用是在save上完成的:
Access-Control-Allow-Head... X-Requested-With, X-Prototype-Version
Access-Control-Allow-Meth... POST, GET, OPTIONS, PUT
Access-Control-Allow-Orig... *
Access-Control-Max-Age 1728000
Cache-Control no-cache
Connection Keep-Alive
Content-Length 0
Content-Type text/plain; charset=utf-8
Date Sat, 13 Oct 2012 22:27:33 GMT
Server WEBrick/1.3.1 (Ruby/1.9.3/2012-04-20)
X-Request-Id a5b58b91a95b72f30849d39db0f0358f
X-Runtime 0.002259
x-ua-compatible IE=Edge
Requêtevoir le code source
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Access-Control-Request-He... content-type
Access-Control-Request-Me... PUT
Connection keep-alive
Host 192.168.20.101:3000
Origin http://localhost:4567
User-Agent Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:16.0) Gecko/20100101 Firefox/16.0
请求在服务器端处理:
Started OPTIONS "/inquiries/2" for 192.168.20.101 at 2012-10-13 18:27:33 -0400
Processing by InquiriesController#options as HTML
Parameters: {"id"=>"2"}
Rendered text template (0.0ms)
Completed 200 OK in 0ms (Views: 0.3ms | ActiveRecord: 0.0ms)
最后,它在保存回调时抛出了这个错误:
readyState 0
responseText ""
status 0
statusText "error"
这是我的控制者:
class InquiriesController < ApplicationController
...
def show
render json: Inquiry.find(params[:id])
end
def create
@inquiry = Inquiry.new(params[:inquiry])
if @inquiry.save
render json: @inquiry, status: :created, location: @inquiry
else
render json: @inquiry.errors, status: :unprocessable_entity
end
end
...
def options
headers['Access-Control-Allow-Origin'] = '*'
headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS, PUT. DELETE'
headers['Access-Control-Allow-Headers'] = 'X-Requested-With, X-Prototype-Version'
headers['Access-Control-Max-Age'] = '1728000'
render :text => '', :content_type => 'text/plain'
end
end
我在ApplicationController上有这个:
class ApplicationController < ActionController::Base
after_filter :cors_set_access_control_headers
protected
def cors_set_access_control_headers
headers['Access-Control-Allow-Origin'] = '*'
headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS, PUT'
headers['Access-Control-Max-Age'] = "1728000"
end
end
这是我的路线:
MyFirstProjectServer::Application.routes.draw do
get "main/index"
root to: "main#index"
resources :inquiries
match '/inquiries', controller: :inquiries, action: :options, constraints: {method: 'OPTIONS'}
match '/inquiry', controller: :inquiries, action: :options, constraints: {method: 'OPTIONS'}
match '/inquiry/:id', controller: :inquiries, action: :options, constraints: {method: 'OPTIONS'}
match '/inquiries/:id', controller: :inquiries, action: :options, constraints: {method: 'OPTIONS'}
end
最后,这是我的模特:
class MyFirstProjectServer.Models.Inquiry extends Backbone.Model
urlRoot:"http://192.168.20.101:3000/inquiries/"
你有解决方案吗?
谢谢!
答案 0 :(得分:0)
解决方案非常简单。
我改变了这个:
headers['Access-Control-Allow-Headers'] = 'X-Requested-With, X-Prototype-Version'
为此:
headers['Access-Control-Allow-Headers'] = 'X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version'