【发布时间】:2012-09-02 10:02:30
【问题描述】:
我需要向不提供 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 被发送。如果我使用其他工具通过 OPTIONS 请求访问服务器,它会按预期工作。
更新:
向http://localhost:7738/post/trackEvent/MoneyInTheBank 发送 OPTIONS 请求来访问服务器会返回:
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
【问题讨论】:
-
看来您的问题是由于 JavaScript 同域问题。见stackoverflow.com/questions/3076414/…