Javascript 轮询xhr.js:229gethttp://localhost:3000/socket.io/?EIO=4&运输=投票及;t=NMtL4rR网络::错误连接被拒绝

Javascript 轮询xhr.js:229gethttp://localhost:3000/socket.io/?EIO=4&运输=投票及;t=NMtL4rR网络::错误连接被拒绝,javascript,html,notifications,Javascript,Html,Notifications,我正在尝试构建一个表单,在提交时用填充的数据发送通知。当我运行代码时,它会在控制台上显示此错误 轮询xhr.js:229gethttp://localhost:3000/socket.io/?EIO=4&transport=polling&t=NMtL4rR 网络::错误连接被拒绝 这个错误看起来像是溢出 这是我的密码 admin.html <!doctype html> <html lang="en"> <head> <!-- Re

我正在尝试构建一个表单,在提交时用填充的数据发送通知。当我运行代码时,它会在控制台上显示此错误

轮询xhr.js:229gethttp://localhost:3000/socket.io/?EIO=4&transport=polling&t=NMtL4rR 网络::错误连接被拒绝

这个错误看起来像是溢出

这是我的密码

admin.html

<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

  <!-- Bootstrap CSS -->
   <link rel="stylesheet" 
  href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" 
  integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" 
  crossorigin="anonymous">

  <title>Hello, admin</title>
  </head>
  <body>
  <div class="container" style="max-width: 500px;">
    <div style="margin-top: 40px;"></div>
    <h1>Send Notification</h1>
    <form>
        <div class="form-group">
          <label for="name">name</label>
          <input type="text" class="form-control" id="name" aria-describedby="emailHelp">
        </div>
        <div class="form-group">
            <label for="profession">profession</label>
            <input type="text" class="form-control" id="profession" aria-describedby="emailHelp">
          </div>
          <div class="form-group">
            <label for="email">Email address</label>
            <input type="email" class="form-control" id="email" aria-describedby="emailHelp">
          </div>
       
       
        <button type="button" id="sendNotification" class="btn btn-primary">Submit</button>
      </form>
  </div>


 <!-- Optional JavaScript; choose one of the two! -->

 <!-- Option 1: jQuery and Bootstrap Bundle (includes Popper) -->
 <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384- 
 DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
 <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js" 
   integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx" 
   crossorigin="anonymous"></script>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/3.0.1/socket.io.js"></script>

   <script type="text/javascript">
    var socket = io("http://localhost:3000");

    $( "#sendNotification" ).click(function() {

        var name = $( "#name" ).val()
        var profession = $( "#profession" ).val()
        var email = $( "#email" ).val()

        socket.emit("sendNotification" ,{
            "name" : name,
            "profession" : profession,
            "email" : email
        });
    });
</script>
</body>
</html>
package.json

 {
 "name": "notification-server",
 "version": "1.0.0",
 "description": "Notification",
 "main": "index.js",
 "scripts": {
"test": "none"
 },
"repository": {
"type": "git",
"url": "none"
 },
"keywords": [
"none"
],
"author": "cbra",
"license": "MIT",
"dependencies": {
"express": "^4.17.1",
"http": "0.0.1-security",
"socket.io": "^3.0.1"
}
}

我必须在这里更改什么?

我想您需要一个
CORS
模块。有两种方法


  • 使用npm
  • npm安装cors
    
    //server.js
    ...
    应用程序使用(要求('cors')())
    ...
    

  • 启用CORS选项(如果您使用的是Socket.IO v3)
  • const io=require(“socket.io”)(http{
    cors:{
    来源:“http://localhost:4200",
    方法:[“获取”、“发布”]
    }
    })
    http.listen(3000)
    
    我想你需要一个
    CORS
    模块。有两种方法


  • 使用npm
  • npm安装cors
    
    //server.js
    ...
    应用程序使用(要求('cors')())
    ...
    

  • 启用CORS选项(如果您使用的是Socket.IO v3)
  • const io=require(“socket.io”)(http{
    cors:{
    来源:“http://localhost:4200",
    方法:[“获取”、“发布”]
    }
    })
    http.listen(3000)
    
    我已解决此错误,将“127.0.0.1:3000”改为“127.0.0.1:3000”https://localhost:3000'在客户端。这是我的密码

    var ip='127.0.0.1:3000'

    
    This should work
    
    var插座=io.connect(ip)


    我已通过将“127.0.0.1:3000”改为“127.0.0.1:3000”解决了此错误https://localhost:3000'在客户端。这是我的密码

    var ip='127.0.0.1:3000'

    
    This should work
    
    var插座=io.connect(ip)

    server.js

    var express = require("express");
    var app = express();
    
    var http = require("http").createServer(app);
    var io = require("socket.io")(http);
    
    http.listen(3000, function(){
    console.log("Successfully Connected Node Server");
    
     io.on("connection", function(socket){
        console.log("Auth value: " + socket.id);
    
        socket.on("sendNotification", function(details){
            socket.broadcast.emit("sendNotification", details);
        });
    });
    });
    
    var express = require("express");
    var app = express();
    
    var http = require("http").createServer(app);
    var io = require("socket.io")(http);
    
    http.listen(3000, function(){
    console.log("Successfully Connected Node Server");
    
     io.on("connection", function(socket){
        console.log(socket)
    
        socket.on("sendNotification", function(details){
            socket.broadcast.emit("sendNotification", details);
        });
    });
    });
    
    上面从服务器端首先检查套接字是否通过登录套接字连接。然后在控制台日志检查中有如下标题:

          host: '127.0.0.1:3000',
          connection: 'keep-alive',
          'sec-ch-ua': '" Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"',
          accept: '*/*',
          'sec-ch-ua-mobile': '?0',
          'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36',
          origin: 'http://localhost:3000',
          'sec-fetch-site': 'cross-site',
          'sec-fetch-mode': 'cors',
          'sec-fetch-dest': 'empty',
          referer: 'http://localhost:3000/',
          'accept-encoding': 'gzip, deflate, br',
          'accept-language': 'en-GB,en-US;q=0.9,en;q=0.8'
        },```
    
    So from console log you can see host is: 127.0.0.1:3000
    
    include socket.io from the root path:
    
    <script src="/socket.io/socket.io.js"></script>
    
    Now on the front end script you can get io this way:
    
    
    server.js

    var express = require("express");
    var app = express();
    
    var http = require("http").createServer(app);
    var io = require("socket.io")(http);
    
    http.listen(3000, function(){
    console.log("Successfully Connected Node Server");
    
     io.on("connection", function(socket){
        console.log("Auth value: " + socket.id);
    
        socket.on("sendNotification", function(details){
            socket.broadcast.emit("sendNotification", details);
        });
    });
    });
    
    var express = require("express");
    var app = express();
    
    var http = require("http").createServer(app);
    var io = require("socket.io")(http);
    
    http.listen(3000, function(){
    console.log("Successfully Connected Node Server");
    
     io.on("connection", function(socket){
        console.log(socket)
    
        socket.on("sendNotification", function(details){
            socket.broadcast.emit("sendNotification", details);
        });
    });
    });
    
    上面从服务器端首先检查套接字是否通过登录套接字连接。然后在控制台日志检查中有如下标题:

          host: '127.0.0.1:3000',
          connection: 'keep-alive',
          'sec-ch-ua': '" Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"',
          accept: '*/*',
          'sec-ch-ua-mobile': '?0',
          'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36',
          origin: 'http://localhost:3000',
          'sec-fetch-site': 'cross-site',
          'sec-fetch-mode': 'cors',
          'sec-fetch-dest': 'empty',
          referer: 'http://localhost:3000/',
          'accept-encoding': 'gzip, deflate, br',
          'accept-language': 'en-GB,en-US;q=0.9,en;q=0.8'
        },```
    
    So from console log you can see host is: 127.0.0.1:3000
    
    include socket.io from the root path:
    
    <script src="/socket.io/socket.io.js"></script>
    
    Now on the front end script you can get io this way:
    
    

    但是如何提供HTML文件呢?服务器代码中没有关于为文件提供服务的内容,例如
    express.static()
    app.get(“/”
    )。如何显示它们?对不起,我刚从youtube视频中准备了这段代码。。。但我的问题的答案是什么?:)但是如何提供HTML文件呢?服务器代码中没有关于为文件提供服务的内容,例如
    express.static()
    app.get(“/”
    )。如何显示它们?对不起,我刚从youtube视频中准备了这段代码。。。但我的问题的答案是什么?:)