0

クライアント側でバックボーンを使用し、サーバー側で Rails を使用してアプリケーションを作成しようとしています。動作しますが、作成、更新、および破棄に問題があります。

私はこれをやっています:

m = new MyFirstProjectServer.Models.Inquiry({id: 1})
m.fetch() #work fine
m.set({title: 'title', description: 'description'}) 
m.save() #don't work

保存時に ajax 呼び出しが行われます。

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/"

解決策はありますか?

ありがとう!

4

1 に答える 1

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'
于 2012-10-15T10:37:39.167 に答える