restify cors 飞行前未连接

Posted

技术标签:

【中文标题】restify cors 飞行前未连接【英文标题】:restify cors pre-flight not connecting 【发布时间】:2013-08-23 06:58:20 【问题描述】:

我们无法在飞行前与 cors 合作:非常感谢任何帮助

========== 错误===========================

About to connect() to localhost port 3000 (#0)
*   Trying ::1...
* Adding handle: conn: 0x9d834e8
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x9d834e8) send_pipe: 1, recv_pipe: 0
* Connection refused
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 3000 (#0)

========卷曲============================

# **** note **** is local....
curl -i -X OPTIONS -H "Access-Control-Request-Method: POST" http://****:3000/api/account 
    -d '"accountname":"test","organization":"test"' -H "Content-Type: application/json" 
    -H "Origin: http://****:8000" -H "Access-Control-Request-Headers: X-Requested-With"
    --verbose

========= 修改代码===================

var restify = require('restify');  
var server = restify.createServer();

var preflight = require('se7ensky-restify-preflight');
preflight(server);

server.post('/api/account', function(req,res,next) 
  console.log("inside account post function");
  console.log("req body " + JSON.stringify(req.body));
  if (!req.body) 
    response.statusCode = 400;
    return res.send("Missing or Invalid Params");
   else 
    res.send('hello ');
  
);

server.listen(3000, function() 
  console.log('%s listening at %s', server.name, server.url);
); 

【问题讨论】:

【参考方案1】:

试过了吗?

server.use(restify.CORS());

【讨论】:

以上是关于restify cors 飞行前未连接的主要内容,如果未能解决你的问题,请参考以下文章

与 API 的连接总是导致 CORS 飞行前错误

Bot Framework Webchat 无法从 Node.js restify 服务器(在 Azure 上)获取令牌:CORS 策略:不存在“Access-Control-Allow-Origin

当提供商因缺少授权标头而拒绝飞行前请求时,让客户端 CORS 工作

Laravel 飞行前 CORS

(CORS) 浏览器如何知道何时进行飞行前请求

为啥这个简单的 CORS 请求会进行飞行前选项检查