Restful-API node.js 和expressCross-Origin 请求被阻止:同源策略不允许读取远程资源的cat。雕像:(空)

问题描述 投票:0回答:1

我使用node.js和express创建了Restful API,当我将API和网站移动到服务器计算机时,当我尝试打开网站时,我在控制台中收到错误:

Restful-API node.js & expressCross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource s cat. Statuode: (null).

// Create express app
var express = require("express")
var app = express()
var Pool = require('pg-pool')
var express = require("express")
var cors = require('cors')
var moment = require('moment');  

app.use(cors());
app.options('*',cors());

// Server port
var HTTP_PORT = 3010


const pool = new Pool(
  {
    connectionLimit: 10,
    host: '192.168.0.1',
    user: 'usr',
    port: '3333',
    password: 'passwd',
    database: 'dbName',
  }
);

var floodguard_stations = '';


app.route('/stations')
  .get(function (req, res) {
    // omitted
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Methods", "GET, PUT, POST");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    //const id2 = req.query.id2;
    
    pool.query(`SELECT * FROM "maritsa_public"."maritsa_codenums";`, function (error, result_stations, fields) {
      if (error)
      return res.status(500).json({ error: "Грешна заявка. Опитай отново !"})
      floodguard_stations = result_stations;
      res.json({ floodguard_stations })
    });
  });

上传到API的服务器有ssl。我通过网站调用所有 URL 链接

https
://192.168.0.1:3333/stations 我可以获得有关如何阻止请求被阻止的指导吗?

node.js express cors
1个回答
0
投票

您必须将 * 传递到此行中的特定域

来自:

res.header("Access-Control-Allow-Origin", "*");

至:

res.header("Access-Control-Allow-Origin", "在此处粘贴您的相同域名");

© www.soinside.com 2019 - 2024. All rights reserved.