标签: cors

当请求的凭据模式为“ include”时,响应中“ Access-Control-Allow-Origin”标头的值不得为通配符“ *”

我试图在Angular和Nodejs服务器之间连接socket.io

在Angular中,我声明了一个新套接字,并从'socket.io-client'中将其导入为io *。... @component ... const socket = io.connect(' http:// localhost:3000 ');

在后端:server.js

const express = require('express');
const app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
io.set('origins', 'http://localhost:4200');

var routes = require('./routes/routes')(io);

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Methods", "GET, POST, PUT ,DELETE");
    res.header(
        "Access-Control-Allow-Headers",
        "Origin, X-Requested-With, Content-Type, Accept"
    );
    next();
});
io.on('connection', function (socket) {
    socket.emit('news', { hello: 'world' });
    console.log("connectd");
});
app.use('/', routes);
var server = app.listen(3000, function (io) …
Run Code Online (Sandbox Code Playgroud)

javascript node.js cors socket.io angular

-1
推荐指数
2
解决办法
4794
查看次数

为什么 Web API 在 chrome、edge 等浏览器以及 Postman 工具中没有给出 CORS 错误?

我正在 Asp.Net Core 3.1 中开发 API。它按预期工作。当我尝试从 ajax 发送请求时,出现了与 CORS 相关的错误。但是当我直接从 Chrome、Edge 等浏览器甚至使用 Postman 等工具发送 GET 请求时,我没有收到任何错误。

错误:

从源“http://localhost:63765”访问“http://server:8080/API/GetMethod?currency=INR”处的 XMLHttpRequest 已被 CORS 策略阻止:没有“Access-Control-Allow-Origin”标头存在于所请求的资源上。

客户端代码:

$.ajax({
   type: 'GET',
   crossDomain: true,
   dataType: 'json',
   url: 'myUrl',
   success: function(jsondata){
   }
})
Run Code Online (Sandbox Code Playgroud)

所以我的问题是为什么它没有在浏览器和邮递员中给出错误。为什么客户端代码会出现错误?浏览器会忽略这些错误吗?根据我的理解,即使浏览器和邮递员也应该给出相同的错误。

更新:虽然链接上下文是相同的,但我对提供的答案不满意。我对现有问题有了更好的具体答案。

javascript ajax jquery cors postman

-1
推荐指数
1
解决办法
1699
查看次数

Node js + Angular 应用程序中的 Socket io 通信被 CORS 策略阻止

因此,我使用带有express和socket.io的简单node.js服务器当我尝试与用javascript编写的简单客户端通信时,它工作得很好,但是当我尝试与Angular应用程序创建通信时(使用ngx-socket- io),我收到以下错误消息:

Access to XMLHttpRequest at 'http://localhost:5000/socket.io/?EIO=3&transport=polling&t=NQFEnVV' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Run Code Online (Sandbox Code Playgroud)

这是nodejs服务器:

Access to XMLHttpRequest at 'http://localhost:5000/socket.io/?EIO=3&transport=polling&t=NQFEnVV' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Run Code Online (Sandbox Code Playgroud)

这就是我在 Angular 客户端上实现 socket.io 的方式:

应用程序模块.ts:

var express = require('express');
var app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const port = process.env.PORT || 5000;
app.set(port, process.env.PORT);
app.use(express.static('./client/')); …
Run Code Online (Sandbox Code Playgroud)

node.js cors socket.io angular

-1
推荐指数
1
解决办法
7273
查看次数

对 Url 的 post 请求给出了被 cors 策略阻止的错误

我正在尝试向 发出帖子请求,URL = "https://sambhav.daily.co/v1/rooms"但我收到了被 cors 政策阻止的错误

addRoom(): Observable<any> {
    const headers = new HttpHeaders()
      .set("content-type", "application/json")
      .set("Access-Control-Allow-Origin", "http://localhost:8000")
      .set("Access-Control-Allow-Methods", "GET,POST,PATCH,DELETE,PUT,OPTIONS")
      .set(
        "Access-Control-Allow-Headers",
        "Origin, Content-Type, X-Auth-Token, content-type"
      )
      .set(
        "Authorization",
        `Bearer <API-KEY>`
      );
    return this.http
      .post(URL, { headers: headers })
      .map((response: any) => response.data)
      .catch(this.handleError);
  }
Run Code Online (Sandbox Code Playgroud)

我收到的错误

Access to XMLHttpRequest at 'https://sambhav.daily.co/v1/rooms' from origin 'http://localhost:8000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Run Code Online (Sandbox Code Playgroud)

cors angular

-1
推荐指数
1
解决办法
90
查看次数

C# 响应中的“Access-Control-Allow-Credentials”标头为“”,当请求的凭据模式为“include”时,该标头必须为“true”

我在 .Net Core 6 中遇到 Cors 问题。尽管 Cors 可以与之前的版本 3.1 配合使用。我搜索了很多没有用。

完整的错误消息是

Access to XMLHttpRequest at 'https://localhost:7230/api/teachers/subjectsByTeacher?teacherId=5&pageNo=1&pageSize=10&sorting=name%20asc' from origin 'https://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Credentials' header in the response is '' which must be 'true' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

这是我的program.cs类:

var builder = WebApplication.CreateBuilder(args);

// Configure cors. …
Run Code Online (Sandbox Code Playgroud)

c# cors angular asp.net-core-6.0

-1
推荐指数
1
解决办法
2744
查看次数

如何从 Sanity 获取数据到 React?

我无法理解如何从理智中获取数据。我已经阅读了文档,但我仍然很困惑。

我尝试将数据记录到控制台,但它给了我一个错误,例如“请求的资源上不存在‘Access-Control-Allow-Origin’标头。”

import React from "react";
import sanityClient from "@sanity/client";

const Post = () => {
  const client = sanityClient({
    projectId: "6sf5fafo",
    dataset: "production",
    useCdn: true
  });
// fetching the data
  client
    .fetch('*[__type == "post"][0]{title, "name": author->name}', {})
    .then(res => {
      console.log("Post info: ", res); // Here is when i tried to log the data but gets an error message.
    })
    .catch(err => {
      console.log(err);
    });
  return (
    <div>
      <h1>Hello</h1>
    </div>
  );
};
export default Post;
Run Code Online (Sandbox Code Playgroud)

有人可以对我的代码进行一些编辑,以正确地从理智中获取数据,我们将非常感激。

cors reactjs sanity

-5
推荐指数
1
解决办法
7733
查看次数