我需要将一些数据发送到不提供javascript的Node服务器。我运行服务器,但从未看到OPTIONS已发送,而Chrome拒绝我的ajax来自其他域名。]
我的服务器:
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);
我的js发送是jqxhr = $.get('localhost:7738/post/trackEvent/' + eventName);
,但我从未看到OPTIONS SING SENT。如果我使用其他工具使用OPTIONS请求命中服务器,它将按预期工作。
更新:
向服务器发送一个OPTIONS请求到http://localhost:7738/post/trackEvent/MoneyInTheBank
会返回:
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