6

I'm trying to execute a POST throw the save method. Here is my model.

app.Models.Dummy = Backbone.Model.extend({
    initialize: function () {
        url = 'http://anotherdomain/Hello/';
    },
});

When i execute:

    dummy.save({text : "greg"}, {
        success : function(){       
            console.log('Ok!');
        }, 
        error: function(){
            console.log('Error');
        }
    });

The request is fired with an OPTIONS header (code 200) but the POST request is never fired. However When i execute:

    $.ajax({
        type: 'POST',
        url: "http://anotherdomain/Hello/",
        data: {text:"greg"},
        success: function(r) { alert(r.Result) },
        dataType: "application/json"
    });

it's works!

Does i need to override something in backbone?

EDIT:

The request is:

OPTIONS http://anotherdomain/Hello/ HTTP/1.1
Host: anotherdomain
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/17.0 Firefox/17.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive
Origin: http://mydomain
Access-Control-Request-Method: POST
Access-Control-Request-Headers: content-type
Pragma: no-cache
Cache-Control: no-cache

and the response is:

HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 0
Server: Microsoft-IIS/7.5
Set-Cookie: ARRAffinity=611c389e4fd6c5d83202b700ce5627f6e0850faf0604f13c25903b4662919f36;Path=/;Domain=anotherdomain
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
X-Powered-By: ARR/2.5
X-Powered-By: ASP.NET
Date: Wed, 05 Dec 2012 18:44:27 GMT
Christian Gollhardt
  • 16,510
  • 17
  • 74
  • 111
Greg
  • 360
  • 3
  • 14
  • what is the full OPTIONS response? note that the backbone documentation says that sometimes model.save() will do a PUT instead of a POST so your service will need to allow that – Robert Levy Dec 05 '12 at 16:26
  • Post edited with the request and the response. – Greg Dec 05 '12 at 18:49
  • 2 years later, I've got the same exact situation. Did you solve this? I've added the headers on my express server, and I've added the crossDomain option to my Backbone.sync. But I can't seem to get past this. It only happens with backbone. Not Postman, and Not Plain jQuery. – Ryan Ore Feb 02 '15 at 13:07
  • @RyanOre Unfortunately I do not remember me . I think I did not use sync () but post() and put() with some hacks... Sorry can not help you more – Greg Feb 25 '15 at 14:01
  • Actually, @Greg I solved my problem by having a global middleware on my Express router which checks for method OPTIONS. If it is, than it allows all domains. Then for the POST request I have a separate middlware that handles specifics. So the fix for me was having OPTIONS being open to all. – Ryan Ore Feb 25 '15 at 19:33

1 Answers1

0

That's not a valid OPTIONS response for CORS. The response needs to include headers that tell the browser what's allowed. For example (taken from MDN):

Access-Control-Allow-Origin: http://foo.example
Access-Control-Allow-Methods: POST, GET, OPTIONS
Access-Control-Allow-Headers: X-PINGOTHER

I know you said that the $.ajax worked but based on this CORS response I doubt that is accurate and suggest you double check. Under the covers, backbone is itself just using $.ajax

Robert Levy
  • 28,747
  • 6
  • 62
  • 94
  • I'm using ServiceStack. I fix it. In the URI, i need to use ?format=json (or define a default format). I got headers with Fiddler. but... with backbone and firebug i don't get them... – Greg Dec 05 '12 at 20:55