net :: ERR_EMPTY_RESPONSE选项-CORS设置是什么? [英] OPTIONS net::ERR_EMPTY_RESPONSE Angular - What are the CORS settings?

查看:127
本文介绍了net :: ERR_EMPTY_RESPONSE选项-CORS设置是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向服务器上的mongoDB发出get请求。这是将与我的网页托管的服务器。我创建了一个API,以便该网页可以向mongoDB发出请求。

I am trying to make a get request to a mongoDB which is on a server. This is the same server that my webpage will be hosted. I have created an API so that the webpage can make requests to the mongoDB.

我的第一个问题是CORS政策阻止了它,因此我研究了此信息我发现导致我在 server.js (服务器上运行的API文件)中添加了 corsOptions

My first issue was with it being blocked by CORS policy so I looked into this and the information I found led to me adding in corsOptions to server.js (the API file running on the server).

这是我的 server.js (我正在服务器上运行的API文件,用于将网页与 node.js 连接到mongoDB):

This is my server.js (The API file I am running on the server to connect the webpage with node.js connect to the mongoDB):

// Dependencies
var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var cors = require('cors');
var app = express();


const corsOptions = {
    origin = true,
    methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
    credentials: true,
    preflightContinue: true,
    maxAge: 600,
};
app.options('*', cors(corsOptions))
app.use(cors(corsOptions))

// MongoDB
mongoose.connect('mongodb://serverip:27017/database');

// Express
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

// Routes
app.use('/api', require('./routes/api'));

// Start server
app.listen(3000);
console.log('Listening on port 3000...');

我的 .component.ts 运行简单 httpClient.get 请求以获取信息,例如:

My .component.ts runs a simple httpClient.get request to get the information, like so:

const httpOptions = {
  headers: new HttpHeaders({
    'Access-Control-Allow-Origin': '*'
  })
};

this.httpService.get('http://serverip:3000/api/collection/', httpOptions).subscribe(
    result => {
        console.log(result);
    },
    error => {
        console.log('Error occured', error);
    }
)

我在Google Chrome浏览器中遇到以下错误:

I get the following error in Google Chrome:

OPTIONS http://serverip:3000/api/collection/ net::ERR_EMPTY_RESPONSE

我已经在 WIN + R +
中使用此命令运行Chrome

I've run Chrome with this command in WIN+R:

chrome.exe --user-data-dir="C:/Chrome dev session" --disable-web-security

在该Chrome上收到此错误:

I get this error on that Chrome:

OPTIONS http://serverip:3000/api/collection/ net::ERR_EMPTY_RESPONSE

如果我删除了与CORS有关的 server.js 中的代码,并在没有网络安全性的情况下将网页加载到Chrome中,然后该网页从MongoDB完全没有问题。

If I remove the code in the server.js that is to do with CORS, and load the webpage in the Chrome with no web security then webpage loads the data from MongoDB with no issues at all.

我还尝试将 OPTIONS 放入方法 server.js 中,但我仍然遇到相同的错误。

I also tried to put OPTIONS into the methods in the server.js but I still get the same error.

我认为此错误来自 corsOptions ,但是我需要更改/添加什么才能解决此错误。

I assume this error is from the corsOptions but what do i need to change/add to resolve this error.

感谢您的帮助!

推荐答案

首先尝试使用Postman测试您的API,以确保您获得正确的响应。第二,尝试添加任何选项。这样可以确保您使用默认的cors选项,然后尝试一个接一个地添加您的选项。

First of all try to test yout API using Postman to ensure you have the correct response. secondly try to add cors withou any option. this ensures you to use default cors options, and then try to add your's one by one.

app.use(cors())

最后,您不需要在http调用中添加任何标题,因此可以删除此部分

lastly, you do not need to add any headr to your http call, so you can remove this part

const httpOptions = {
  headers: new HttpHeaders({
    'Access-Control-Allow-Origin': '*'
  })
};

这篇关于net :: ERR_EMPTY_RESPONSE选项-CORS设置是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆