簡體   English   中英

Socket.io沒有'Access-Control-Allow-Origin'標頭出現在請求的資源上。 因此不允許Origin'http:// localhost'訪問

[英]Socket.io No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access

我正在嘗試使用socket.io學習nodejs,目前我正在使用GianlucaGuarini的這個教程 輸入我的client.html文件時,出現以下錯誤。 我知道這意味着什么,這是為了防止跨瀏覽器腳本,但我不知道如何允許我的nodejs腳本訪問client.html文件。

XMLHttpRequest cannot load http://localhost:8000/socket.io/?EIO=3&transport=polling&t=1422653081432-10. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.

這是我的socket代碼的一部分。

  var app = require('http').createServer(handler),
  io = require('socket.io').listen(app),
  fs = require('fs'),
  mysql = require('mysql'),
  connectionsArray = [],
  connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '',
    database: 'database',
    port: 3306
  }),
  POLLING_INTERVAL = 3000,
  pollingTimer;

// If there is an error connecting to the database
connection.connect(function(err) {
  // connected! (unless `err` is set)
  console.log(err);
});

// creating the server ( localhost:8000 )
app.listen(8000);

// on server started we can load our client.html page
function handler(req, res) {

  res.writeHead(200, {
      /// ...
      'Access-Control-Allow-Origin' : '*'
  });

  fs.readFile(__dirname + '/client.html', function(err, data) {
    if (err) {
      console.log(err);
      res.writeHead(500);
      return res.end('Error loading client.html');
    }
    res.writeHead(200);
    res.end(data);
  });
}

有誰知道如何解決我的問題?

善意/ H

首先 - 停止使用writeHead到處。 因為它重寫了完整的響應頭。

如果游覽寫得像這樣:

res.writeHead(200,{"coolHeader":"YesIAm"});
res.writeHead(500);

然后node.js將發送狀態為500且沒有標題“coolHeader”的響應;

如果您想更改狀態代碼,請使用

res.statusCode = ###;

如果你想添加新的標題使用

res.setHeader("key", "value");

如果你想重寫所有標題然后使用writeHeader(...)

第二。 添加此代碼

res.statusCode = 200;
//...
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");

而不是你的

 res.writeHead(200, {
      /// ...
      'Access-Control-Allow-Origin' : '*'
  });

並用res.statusCode = ###;替換所有writeHead(###) res.statusCode = ###;

嘗試在Node中的響應對象上設置`Access-Control-Allow-Origin'標頭。

response.writeHead(200, {
        /// ...
        'Access-Control-Allow-Origin' : '*'
    });

看起來你正在為應用程序和socket.io調用.listen(我認為這是多余的,因為你使用socket.io擴展你的服務器)

我有一個小塊對我使用socket.io 1.x我很喜歡使用https,因為它殺死了防火牆和防病毒的一些問題,但這個例子被重寫為http。

var http = require('http'),
    socketio = require('socket.io'),
    options={},
    port=8080;


//start http
var app = http.createServer(options, handler),
    io = socketio(app, {
        log: false,
        agent: false,
        origins: '*:*'
        // 'transports': ['websocket', 'htmlfile', 'xhr-polling', 'jsonp-polling']
    });

app.listen(port);
console.log('listening on port ' + port);

暫無
暫無

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

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