【问题标题】:Backbone model.save() is causing OPTIONS not POSTBackbone model.save() 导致 OPTIONS not POST
【发布时间】:2014-10-05 16:43:46
【问题描述】:

我正在着手处理“创建帐户”视图。 Backbone 1.1.2 (typescript) 前端,Rails 4.2 beta 1 Web 服务后端。

帐户模型

export class Account extends Backbone.Model {
    public urlRoot: string;
    public validation:any;

    constructor(attributes?: any, options?: any){
        this.urlRoot = 'http://domain.fake/accounts';
        this.validation = {
            email: {
                required: true,
                pattern: 'email'
            },
            password: {
                required: true,
                minLength: 6
            }
        };
        super(attributes, options);
    }
}

创建帐户视图:

export class CreateAccountView extends Backbone.View {
    public template: string;
    public events: any;
    public model: accountModelImport.Account;

    constructor(options?: Backbone.ViewOptions){
        this.el = '#modal';
        this.template = createAccountViewTemplate;
        this.model = new accountModelImport.Account();
        this.events = {
            'click #create-account-submit' : 'create'
        };
        super(options);
    }

    public render(): CreateAccountView {
        this.$el.html(_.template(this.template));
        Backbone.Validation.bind(this);
        this.$el.modal('show');
        return this;
    }

    public create(){
        var email:string = $('#create-account-email').val(), password:string = $('#create-account-password').val(), passconf:string = $('#create-account-password-confirmation').val();
        this.model.set({email: email, password: password, password_confirmation: passconf});
        this.model.save(null, {success: this.success, error: this.error});
    }

    public success(){
        alert('Success');
    }

    public error(){
        alert('error');
    }
}

Rails 从上方输出model.save()

ActionController::RoutingError (No route matches [OPTIONS] "/accounts"):

我已经看到很多关于将什么作为第一个参数传递给 .save() 的问题,并且我每次都尝试过它们,结果都是相同的:null, false, {}

我已尝试搜索具有相同问题的问题,但无法找到。在我走上覆盖.sync() 方法的道路之前,我想尝试让它在本地工作。

为什么.save() 尝试使用OPTIONS 而不是POST

【问题讨论】:

    标签: ruby-on-rails backbone.js typescript backbone-model


    【解决方案1】:

    为什么 .save() 尝试使用 OPTIONS 而不是 POST?

    不是。这是 CORS 的“预检请求”。如果 OPTIONS 请求成功,则 POST 请求随之而来。

    ...预检请求以 HTTP OPTIONS 请求的形式发出(因此 确保您的服务器能够响应此方法)。它还包含 一些额外的标题:

    Access-Control-Request-Method - 实际请求的 HTTP 方法。 始终包含此请求标头,即使 HTTP 方法是 前面定义的简单 HTTP 方法(GET、POST、HEAD)。

    Access-Control-Request-Headers - 逗号分隔的非简单列表 请求中包含的标头。

    预检请求是一种请求实际权限的方式 请求,在发出实际请求之前。服务器应该检查 上面的两个标头来验证 HTTP 方法和 请求的标头有效并被接受。

    http://www.html5rocks.com/en/tutorials/cors/

    【讨论】:

      【解决方案2】:

      正如@meagar 在他对这个问题的回答中所指出的,这并不是骨干试图做错的事情。这是一个 CORS 问题。我使用config.action_dispatch.default_headers.merge! 手动设置了标题,但显然这还不够。

      再用谷歌搜索一下,我发现了这个小宝石的答案(明白了,'gem')... Rails RoutingError (No route matches [OPTIONS]

      那里的答案将我引向https://github.com/cyu/rack-cors

      安装 gem 并按照他们的说明进行配置后,POST 请求按预期通过。

      希望这对未来的其他人有所帮助,并一定要感谢 @meagar 帮助我走上正确的道路。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-26
        • 1970-01-01
        • 1970-01-01
        • 2012-07-03
        • 2012-07-04
        相关资源
        最近更新 更多