简体   繁体   English

节点 CORS 策略:请求的资源上不存在“Access-Control-Allow-Origin”header

[英]Node CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource

I am using two server of port 8080 and 8082, where 8082 works on client side and 8080 acts as a middleware.我正在使用端口 8080 和 8082 的两台服务器,其中 8082 在客户端工作,而 8080 作为中间件。 I am making a http callout from server to middleware using a html button, but it gives an error " Access to XMLHttpRequest at ' http://localhost:8080/ ' from origin ' http://localhost:8082 ' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. " I am making a http callout from server to middleware using a html button, but it gives an error " Access to XMLHttpRequest at ' http://localhost:8080/ ' from origin ' http://localhost:8082 ' has been blocked by CORS 策略:请求的资源上不存在“Access-Control-Allow-Origin”header。

Attaching the code: Server.js附上代码: Server.js

var http = require('http');  
var url = require('url');  
var fs = require('fs');  
var cors = require('cors');
var express = require('express');
var app = express();
app.use(cors());
var server = http.createServer(function(request, response,next) {  
    var path = url.parse(request.url).pathname;  
    switch (path) {  
        case '/':  
            response.writeHead(200, {  
                'Content-Type': 'text/plain'  
            });  
            response.write("This is Test Message.");  
            response.end();  
            break;  
        case '/server.html':  
            fs.readFile(__dirname + path, function(error, data) {  
                if (error) {  
                    response.writeHead(404);  
                    response.write(error);  
                    response.end();  
                } else {

                    response.writeHead(200, {  
                        'Content-Type': 'text/html'  
                    });  
                    response.write(data);  
                    response.end();  
                }  
            });  
            break;  
        case '/HtmlPage2.html':  
            fs.readFile(__dirname + path, function(error, data) {  
                if (error) {  
                    response.writeHead(404);  
                    response.write(error);  
                    response.end();  
                } else {  
                    response.writeHead(200, {  
                        'Content-Type': 'text/html'  
                    });  
                    response.write(data);  
                    response.end();  
                }  
            });  
            break;  
        default:  
            response.writeHead(404);  
            response.write("opps this doesn't exist - 404");  
            response.end();  
            break;  
    }  
});  
server.listen(8082); 

Server.html服务器.html

<!DOCTYPE html>
<html>
  <head>
        <script type="text/javascript" src="server.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
        <script>
          function callServer(){
            $.get('http://localhost:8080', function(returnResult) {

            });
            }  
            </script>
    </head>
<body>
        <button onclick="callServer()">Click</button>
</body>

MiddleWareServer.js MiddleWareServer.js

var http = require('http');  
var url = require('url');  
var fs = require('fs');  
var cors = require('cors');
var express = require('express');
var app = express();
app.use(cors());

var server = http.createServer(function(request, response,next) {   
    response.writeHead(200, {  
        'Content-Type': 'text/plain'  
    });  
    function returnResult(){
        console.log('Called from the port:8082');
    }
    response.write("Port 8080 started..");  
    response.end();  
});  
server.listen(8080);  

That is not how you use express.这不是你使用快递的方式。

MiddleWareServer.js MiddleWareServer.js

var cors = require('cors');
var express = require('express');
var app = express();
app.use(cors());

app.get('/', function(request, response, next) {   
    response.send("Port 8080 started.."); 
})

express.listen(8080);

Sending a get request to localhost:8080 would return Port 8080 started.. .localhost:8080发送 get 请求将返回Port 8080 started..

Same goes for Server.js . Server.js也是如此。

Just used Express and it worked.. Replacing and posting new files: These file will fulfill the following features:刚刚使用 Express 并且它有效.. 替换和发布新文件:这些文件将实现以下功能:

  1. Create and start two different server.创建并启动两个不同的服务器。
  2. Call the middleware server from a html file from 8082 port.从 8082 端口的 html 文件调用中间件服务器。
  3. Fetch the response in html file from middle ware server.从中间件服务器获取 html 文件中的响应。

server.js服务器.js

const express = require('express');
const app = express();
const path = require('path');
const router = express.Router();
var cors = require('cors');
app.use(cors());
router.get('/server',function(req,res){
  res.sendFile(path.join(__dirname+'/server.html'));
});
app.use('/', router);
app.listen(8082);
console.log('Running at Port 8082');

MiddleWareServer.js MiddleWareServer.js

const express = require('express');
const app = express();
var cors = require('cors');
app.use(cors());
app.get('/', (req, res) => res.send('Localhost:8080 has been Started'));
app.post('/middlewarehit', function(req,res){
    res.end('Localhost:8080 Hit');
    } 
);
app.listen(8080);
console.log('Running at Port 8080');

server.html服务器.html

<!DOCTYPE html>
<html>
  <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
        <script>
          function callServer(){
            $.ajax({
              url: "http://localhost:8080/middlewarehit",
              type: "POST",
              dataType: '',
              success: function(res){
                $('#response').val(res);
              }
          });
        }  
            </script>
    </head>
<body>
        <input type="text" id="response">
        <button onclick="callServer()">Click</button>
</body>
</html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular Socketio nodejs - 被 CORS 策略阻止:请求的资源上不存在“Access-Control-Allow-Origin”header - Angular Socketio nodejs - blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource CORS 策略返回 No Access-Control-Allow-Origin' header 存在于 Express Gateway 中请求的资源上 - CORS policy returning No Access-Control-Allow-Origin' header is present on the requested resource in Express Gateway Nodejs React CORS 策略:请求的资源上不存在“Access-Control-Allow-Origin”header - Nodejs React CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource ExpressJS:请求已被CORS策略阻止:请求的资源上不存在“ Access-Control-Allow-Origin”标头 - ExpressJS: Request has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource CORS政策:所请求的资源上没有“ Access-Control-Allow-Origin”标头 - CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource CORS 策略错误:请求的资源上不存在“Access-Control-Allow-Origin”标头 - CORS policy Error: No 'Access-Control-Allow-Origin' header is present on the requested resource Sails、React 和 Axios CORS 策略出错:请求的资源上不存在“Access-Control-Allow-Origin”header - Sails, React and Axios Error with CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource 上传文件时:CORS 策略:没有“Access-Control-Allow-Origin” header 出现在请求的资源上 - While uploading files: CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource 来自源的 XMLHttpRequest 已被 CORS 策略阻止:请求的资源上不存在“Access-Control-Allow-Origin”标头 - XMLHttpRequest at from origin has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource 当服务被 cors 策略阻止时如何修复:请求的资源上不存在“access-control-allow-origin”标头 - How to fix when a service has been blocked by cors policy: no 'access-control-allow-origin' header is present on the requested resource
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM