0

I need to send some data to a Node server which is not serving the javascript. I run the server, but never see OPTIONS sent and Chrome is rejecting my ajax for being from a different domain.]

My server:

var http = require('http');
var router = require('router');
var routing = router();
var server = http.createServer(routing);
routing.options('*', function(request, response){
  console.log("OPTIONS BEING SENT");
  var origin = (request.headers.origin || "*");
  response.writeHead("204",
                     "No Content",
                     { "access-control-allow-origin": origin,
                     "access-control-allow-methods": "GET, POST, PUT, DELETE, OPTIONS",
                     "access-control-allow-headers": "content-type, accept",
                     "access-control-max-age": 10,
                     "content-length": 0
                   });
  response.end();
});

server.listen(7738);

my js sends is jqxhr = $.get('localhost:7738/post/trackEvent/' + eventName); but I never see OPTIONS BEING SENT. If I use another tool to hit the server with an OPTIONS request, it works as expected.

UPDATE:

Hitting the server with an OPTIONS request to http://localhost:7738/post/trackEvent/MoneyInTheBank returns this:

HTTP/1.1 204 No Content
Content-Length: 0
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, PUT, OPTIONS
Access-Control-Allow-Headers: Content-Type, Accept
Access-Control-Max-Age: 10
Connection: close
Chris
  • 11,819
  • 19
  • 91
  • 145
  • It appears your problem is due to JavaScript same domain issues. See http://stackoverflow.com/questions/3076414/ways-to-circumvent-the-same-origin-policy – Todd Moses Sep 09 '12 at 04:08

1 Answers1

2

only preflight request sends OPTIONS header, and since your ajax request is a simple method with simple header, no OPTIONS is sent to server.

for details check W3C and MDN

Ted Shaw
  • 2,298
  • 14
  • 8
  • I changed the method to a PUT but it still complains that `OPTIONS localhost:7738/post/trackEvent/MoneyInTheBank Resource failed to load `. Why would this be if I can ping that address with OPTIONS and it behaves as desired? – Chris Sep 09 '12 at 18:59
  • Wiping up a simple Rails app fixes that. Chrome just doesn't like the origin coming from a file. – Chris Sep 09 '12 at 20:45