Unable to make an update on the server with an id

后端 未结 2 703
情书的邮戳
情书的邮戳 2021-01-27 09:52

I am updating a form and i want to make an update request on the serverwith an id my model is:

    var CampaignEditModel = Backbone.Model.extend({
    urlRoot:\         


        
相关标签:
2条回答
  • 2021-01-27 10:02

    Instead of passing options to your custom render(options) function and setting the model id there, set the it directly on the editCampaigns model, before entering render(options):

    editCampaigns.set('id', id);
    $contents.empty().append(new EditView({model:editCampaigns}).render().el);
    

    and remove the extra

    this.model.set({"id":options.id})
    

    from render(options) together with the options parameter. It should look like similar to this:

    render: function(){
        this.$el.append( _.template(EditTemplate));
        console.log(this.model.get("id"));
        this._modelBinder.bind(this.model, this.el);
        return this;
    }
    

    Your model also has an extra url function:

    url : function(){
       var url = this.urlRoot + this.id;
        return url;
    }
    

    you don't need this one since the models' id is automatically appended after urlRoot.


    Unrelated to you problem I see you used

    http://localhost:3033/campaign/update
    

    to define your update URL. The HTTP method you use, already says what kind of action will be executed, this is the reason why you can (and should) write URLs without verbs. Just remove the extra /update.

    Here is a quick summary about best-practices: How to create REST URLs without verbs?

    0 讨论(0)
  • 2021-01-27 10:14

    Double check that the request is a post request and not a put request. 'Failed to load resource' errors is usually related to a missing request handler.

    0 讨论(0)
提交回复
热议问题