1

I am currently working on a backbone application, where you have to specify a guest name, in order to enter.

  Guest = Backbone.Model.extend({

    urlRoot : ftd.settings.rest + '/guest',

    defaults: {
      name : '',
    },

    validate: function( attr ) {
      var errors = [];
      if(attr.name.length < 4) {
        errors.push({message : "You nickname, must be atleast 4 chars", name : 'guestNickname'});
      }

      if(errors.length > 0) {
        return errors;
      }
    }
  });

  return Guest;

So on my frontpage the user sets a username and a new guest is instantiated, this is my front page view that handles the guest creation.

createGuest: function( ev ) {
  ev.preventDefault();
  // Get nickname.
  var guest = new Guest();
  guest.bind( 'error', function( model, errors ) {
        _.each( errors, function( err ) {
      $('input[name=' + err.name + ']').addClass('invalid');
      // add a meesage somewhere, using err.message
          }, this );
  });
  guest.save({'name' : $('input[name="guestNickname"]').val()}, {
    success:function(model, response) {
      console.log('Successfully saved!');
    },
    error: function(model, error) {
      console.log(model);
      console.log(error.error());
    }
  });
},

My problem is that, when backbone makes the request, it sends a OPTIONS request without the specified name, i even checked the packet in Wireshark, what am i doing wrong?

Bonus question: Why is backbone sending a OPTIONS request?

MartinElvar
  • 5,695
  • 6
  • 39
  • 56
  • 2
    Are you trying to send something cross domain? That's what triggers the OPTIONS request. http://stackoverflow.com/questions/1099787/jquery-ajax-post-sending-options-as-request-method-in-firefox – WiredPrairie Feb 16 '13 at 19:35
  • I am in fact, but only at my local enviroment. Thank you, i will have a look at the link. Hopefully it work within backbone :) – MartinElvar Feb 16 '13 at 20:02

1 Answers1

0

Apparently Backbone (or really your browser) sends an OPTIONS request during a backbone save with side effects because it tries to emulate "true" REST including PUT and DELETE requests, and not all servers support those commands.

According to this:

http://backbonejs.org/#Sync-emulateHTTP

If you set

Backbone.emulateHTTP = true;

then it will just try to POST.

lmortenson
  • 1,610
  • 11
  • 11
  • If i understand right, that only concerns PUT & DELETE request, but when you create a new entity, the type is POST. Also it did't seem to make a difference. :( – MartinElvar Feb 16 '13 at 18:39