gpt4 book ai didi

node.js - nodeS CORS 的问题

转载 作者:太空宇宙 更新时间:2023-11-04 02:08:54 25 4
gpt4 key购买 nike

我试图用 NODEJS 显示 dicom,但浏览器向我抛出这个问题

XMLHttpRequest can not load http: // localhost: 8080 / wado? RequestType = WADO & studyUID = 1.2.840.113704.1.111.5 ... 26513.429 & contentType = application% 2Fdicom & transferSyntax = 1.2.840.10008.1.2. In 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http: // localhost: 3000' is therefore not allowed access.

然后安装 npm 包 cors

在这里,我将代码部分保留在 app.js 中

Var express = require ('express');
Var cors = require ('cors')
App.use (cors ())


App.listen (3000, function () {
  Console.log ('listening on 3000')
})

另外尝试在 app.js 中添加它

App.use (function (req, res, next) {
  Res.header ("Access-Control-Allow-Origin", "*");
  Res.header ("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  Next ();
});

但是浏览器仍然显示错误:

XMLHttpRequest can not load http: // localhost: 8080 / wado? RequestType = WADO & studyUID = 1.2.840.113704.1.111.5 ... 26513.429 & contentType = application% 2Fdicom & transferSyntax = 1.2.840.10008.1.2. In 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http: // localhost: 3000' is therefore not allowed access.

我有疑问,因为我也使用了我留在此处的路由index.js

var express = require('express');
var router = express.Router();
var cors = require('cors')
/*var express = require('express');
var router = express.Router();*/
var mysql = require('mysql');
router.use(cors({origin: 'http://localhost:3000'}));
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});

module.exports = router;

对不起我的英语

最佳答案

出于安全目的,浏览器在正常情况下不允许跨源请求。

您的页面正在从 localhost:3000 加载,它定义了来源,因为它是第一个请求。您的代码正在尝试从端口 8080 上运行的另一个服务加载数据,该服务与源不同。因此基本上您需要在端口 8080 上运行的服务上启用 cors,因为它是外部源。

关于node.js - nodeS CORS 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43131076/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com