我最近想用ExtJS 4.2.2作为前端框架实现rails4 API。
-Rails 4使用REST ==> http://localhost:3000
-ExtJS也使用rest proxy ==> http://localhost:1841
所以当对rails服务器的请求时,它会考虑跨域
当我使用ExtJS rest代理从rails服务器检索数据时它工作正常,但是当我使用POST,PUT或DESTROY方法服务器,服务器响应代码200(ok)到客户端时,它很奇怪,但是客户端没有&# 39;获得响应200后,再次向服务器重新发送实际请求。
服务器(应用程序控制器)
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_filter :cors_preflight_check
after_filter :cors_set_access_control_headers
# For all responses in this controller, return the CORS access control headers.
def cors_set_access_control_headers
headers['Access-Control-Allow-Origin'] = '*'
headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS, PATCH, PUT, DELETE, HEAD'
headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, X-Prototype-Version, content_Type, Accept'
headers['Access-Control-Max-Age'] = "1728000"
end
def cors_preflight_check
if request.method == "OPTIONS"
headers['Access-Control-Allow-Origin'] = '*'
headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS, PATCH, PUT, DELETE, HEAD'
headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, X-Prototype-Version, content_Type, Accept'
headers['Access-Control-Max-Age'] = '1728000'
render :text => '', :content_type => 'text/plain'
end
end
end
服务器(路由):
BnDERP::Application.routes.draw do
root "public#index"
get "show/:permalink", :to => 'public#show'
match "admin", :to => "access#index", via: [:get,:post]
get ':controller(/:action(/:id))'
post ':controller(/:action(/:id))'
match '*all' => 'application#cors_preflight_check', :constraints => {:method => 'OPTIONS'}, via: [:options]
resources :sections
end
ExtJS商店:
Ext.define('ERP.store.Sections', {
extend: 'Ext.data.Store',
model: 'ERP.model.Section',
autoLoad: true,
proxy: {
type: 'rest',
url: 'http://localhost:3000/sections',
reader: {
type: 'json',
root: 'sections',
successProperty: 'success'
}
}
});
服务器日志:
使用GET时:服务器接收OPTIONS方法==&gt;回复代码200(ok)==&gt;客户端使用GET方法重新发送实际请求
Started OPTIONS "/sections?_dc=1394515993496&page=1&start=0&limit=25" for 127.0.0.1 at 2014-03-11 12:33:13 +0700
ActiveRecord::SchemaMigration Load (0.3ms) SELECT `schema_migrations`.* FROM `schema_migrations`
Processing by ApplicationController#cors_preflight_check as */*
Parameters: {"_dc"=>"1394515993496", "page"=>"1", "start"=>"0", "limit"=>"25", "all"=>"sections"}
Rendered text template (0.1ms)
Filter chain halted as :cors_preflight_check rendered or redirected
Completed 200 OK in 78ms (Views: 17.6ms | ActiveRecord: 0.0ms)
Started GET "/sections?_dc=1394515993496&page=1&start=0&limit=25" for 127.0.0.1 at 2014-03-11 12:33:14 +0700
Processing by SectionsController#index as */*
Parameters: {"_dc"=>"1394515993496", "page"=>"1", "start"=>"0", "limit"=>"25"}
Section Load (0.0ms) SELECT A.*,B.name AS Page FROM sections AS A LEFT JOIN pages AS B ON A.page_id=B.id
Completed 200 OK in 112ms (Views: 6.3ms | ActiveRecord: 5.6ms)
使用PUT或DESTROY时:服务器接收OPTIONS方法==&gt;回复代码200(ok)==&gt;客户不发送实际请求
Started OPTIONS "/sections/3?_dc=1394516006492" for 127.0.0.1 at 2014-03-11 12:33:26 +0700
Processing by ApplicationController#cors_preflight_check as */*
Parameters: {"_dc"=>"1394516006492", "all"=>"sections/3"}
Rendered text template (0.0ms)
Filter chain halted as :cors_preflight_check rendered or redirected
Completed 200 OK in 6ms (Views: 5.5ms | ActiveRecord: 0.0ms)
这些问题发生了什么?或者我错过了别的什么?
任何帮助都将不胜感激。