2014년 12월 15일 월요일

[backbonejs] Backbone.js Sends OPTIONS request

When I send .create() from collection, it's always send OPTIONS (only in Google Chrome) request method.

Someone know how can i solve this?

I'm trying enable headers in my back-end service, but doesn't work.



I don't think Backbone itself is doing this—it never sends OPTIONS to my knowledge. This doesn't happen for me in Chrome.

A quick search turned up this, might be your problem:

Hope that helps,



I saw this thread, but doesn't work. I allow all response header in my back-end.

response.header("Access-Control-Allow-Origin", "*");
        response.header("Access-Control-Allow-Headers", "Accept, Accept-Encoding, Accept-Language, Access-Control-Request-Headers, Access-Control-Request-Method, Connection, Host, Origin, Referer, User-Agent");
        response.header("Access-Control-Allow-Methods", "GET, POST, PUT, UPDATE, OPTIONS");


and my Backbone.view is so simple (test) :

'use strict';

define(['jquery', 'underscore', 'backbone', 'text!template/app.html', 'collection/justification-collection'], 
function($, _, Backbone, appTmpl, JustificationCollection){
    var AppView = Backbone.View.extend({
        
        el: '#app',
        
        initialize: function() {
            this.template = appTmpl;
            this.$el.html(this.template);
            this.collection = new JustificationCollection();
        },
        
        events: {
            'click .send': 'call'
        },
        
        call: function() {
            this.collection.create({
                'ju_name': 'test',                
                success: function(response) {
                    console.log("Success: " +JSON.stringify(response));
                },
                
                error: function(response) {
                    console.log("Error: " + JSON.stringify(response));
                }
            });
        }
    }); 
    
    return AppView;
});


When I test in IE(right in IE) and Opera it's works fine but chrome no



The only time I’ve seen this is when performing AJAX requests cross-domain. This isn’t a backbone issue, it’s a JQuery issue. This discussion on Stack Overflow may help: http://stackoverflow.com/questions/1099787/jquery-ajax-post-sending-options-as-request-method-in-firefox



I also saw this discussion. But nothing work for me, I'm afraid about that haha 



Are you doing cross domain requests? (I.e., are the requests targeted to a domain different than the one in which the client app is running?)

An OPTIONS HTTP request is to be expected as part of 'preflighted' cross domain requests. It's normal. Take a look athttps://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Preflighted_requests for more information. If this is indeed the case, make sure you're handling the OPTIONS requests on your server side (by simply replying with a 200 OK for example) as the browser will not proceed to the actual POST request unless the preflight OPTIONS requests succeeds (with the expected Access-Control headers).

I Hope this helps,



Yeah, I'm doing cross domain requests. 

Sure. I'll see that and test!



Solve my issue. Always when you will make a cross domain request make sure that you have OPTIONS request and allow headers.


댓글 없음:

댓글 쓰기