2017-08-02 128 views
0

林試圖連接到一個僅API,但沒有工作,是給我的一個錯誤:無法連接到插座內外部服務器(API)10

XMLHttpRequest cannot load https://api.sports/socket.io/?api_key=aredasdasdadds&EIO=3&transport=polling&t=LsYSPsv. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed access. The response had HTTP status code 596. 

奇怪的是錯誤的表現一個不同的API網址。

我的代碼是:

<!DOCTYPE html> 
<html> 
<head> 
    <title></title> 
</head> 
<body> 

<h1>Web socket</h1> 
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.3/socket.io.js"></script> 
<script type="text/javascript"> 


    // Create SocketIO instance, connect 

    var socket = new io('https://api.sports.com/soccer-/eu/en/matches/summary.json?api_key=aredasdasdadds'); 
    socket.connect(); 

    // Add a connect listener 
    socket.on('connect',function() { 
     console.log('Client has connected to the server!'); 
    }); 
    // Add a connect listener 
    socket.on('message',function(data) { 
     console.log('Received a message from the server!',data); 
    }); 


    // Sends a message to the server via sockets 
    function sendMessageToServer(message) { 
     socket.send(message); 
    } 
</script> 
</body> 
</html> 
+0

該URL看起來不像是Socket.IO端點。你正在試圖找回那個網址嗎?如果是這樣,請使用['fetch'](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch)。 – robertklep

回答

0

你是什麼服務器?

首先,你必須檢查客戶端和服務器上的socket.io版本是否相同(我在客戶端看到的是2.0.3)。

其次,你只需要使用你的網站的域名是這樣的:

var socket = io.connect('ws://https://api.sports.com:8443/', {transports: ['websocket']}); 

,並設置端口監聽,通常是爲8443 HTTPS。

問候!