【问题标题】:Probleme on update, save and delete between Backbone and Rails在 Backbone 和 Rails 之间更新、保存和删除的问题
【发布时间】:2012-10-15 18:37:39
【问题描述】:

我正在尝试在客户端创建一个具有骨干网的应用程序,并在服务器端创建一个 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/"

你有解决办法吗?

谢谢!

【问题讨论】:

    标签: ruby-on-rails backbone.js cors


    【解决方案1】:

    解决方案非常简单。

    我改变了这个:

    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'
    

    【讨论】:

      猜你喜欢
      • 2017-08-21
      • 2014-11-01
      • 1970-01-01
      • 2018-01-31
      • 2020-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多