簡體   English   中英

CORS與Node.js和jQuery

[英]CORS with Node.js and jQuery

我需要將一些數據發送到不提供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返回:

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

只有預檢請求發送OPTIONS標頭,並且因為你的ajax請求是一個帶有簡單標題的簡單方法,所以沒有OPTIONS被發送到服務器。

有關詳細信息,請檢查W3CMDN

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM