-1

I'm trying to fetch from a REST endpoint using a model. Here's the code:

professors: function(id) {
  professor = new ProfessorModel({
    id: id
  });

  professor.fetch({
    headers: {
      'HTTP_ACCESS_TOKEN': document.cookie
    },

    success: function(model, response, options) {
      AppController.showView(new ProfessorView({model: model}));
    },

    error: function(model, response, options) {
      AppController.showView(new ErrorView({
        statusCode: response.status,
        errorMessage: response.statusText
      }));
    }
  });
}

For some reason, the REST endpoint is telling me that the fetch is using OPTIONS instead of GET.

I tried this answer, but it didn't work. CORS is already enabled on my endpoint and the Backbone.enableHTTP option didn't work either.

I looked at the Backbone source and I can't seem to find anything about it using OPTIONS to make any requests. Anybody have any ideas?

Hugo
  • 2,186
  • 8
  • 28
  • 44
  • What you are experiencing is a CORS preflight. Next time read the [manual](http://www.w3.org/TR/cors/#cross-origin-request-with-preflight-0), it is not that long. – inf3rno Mar 18 '15 at 18:37
  • @inf3rno I asked for your help, not your attitude. – Hugo Mar 18 '15 at 18:42

1 Answers1

1

The OPTIONS request is an underlying feature of XMLHttpRequest trigged by your use of a custom HTTP header ('HTTP_ACCESS_TOKEN': document.cookie).

It isn't specific to Backbone, which is why you couldn't find a reference to it in the Backbone source code.

The answer you found is the right one. You need to set up your server to correctly respond to the OPTIONS request.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335