相关疑难解决方法(0)

为什么我的JavaScript在所请求的资源上出现"No'Access-Control-Allow-Origin'标头"错误,当Postman没有?

我试图通过连接到Flask内置的RESTful API来使用JavaScript进行授权.但是,当我发出请求时,我收到以下错误:

XMLHttpRequest无法加载http:// myApiUrl/login.请求的资源上不存在"Access-Control-Allow-Origin"标头.因此不允许原点'null'访问.

我知道API或远程资源必须设置标题,但为什么在我通过Chrome扩展程序Postman发出请求时它可以正常工作?

这是请求代码:

$.ajax({
    type: "POST",
    dataType: 'text',
    url: api,
    username: 'user',
    password: 'pass',
    crossDomain : true,
    xhrFields: {
        withCredentials: true
    }
})
    .done(function( data ) {
        console.log("done");
    })
    .fail( function(xhr, textStatus, errorThrown) {
        alert(xhr.responseText);
        alert(textStatus);
    });
Run Code Online (Sandbox Code Playgroud)

javascript jquery same-origin-policy cors flask-restless

2320
推荐指数
29
解决办法
399万
查看次数

如何在node.js中创建一个简单的http代理?

我正在尝试创建一个代理服务器,将HTTP GET客户端的请求传递给第三方网站(比如google).我的代理只需要将传入的请求镜像到目标站点上的相应路径,因此如果我的客户端请求的URL是:

127.0.0.1/images/srpr/logo11w.png
Run Code Online (Sandbox Code Playgroud)

应提供以下资源:

http://www.google.com/images/srpr/logo11w.png
Run Code Online (Sandbox Code Playgroud)

这是我想出的:

http.createServer(onRequest).listen(80);

function onRequest (client_req, client_res) {
    client_req.addListener("end", function() {
        var options = {
            hostname: 'www.google.com',
            port: 80,
            path: client_req.url,
            method: client_req.method
            headers: client_req.headers
        };
        var req=http.request(options, function(res) {
            var body;
            res.on('data', function (chunk) {
                body += chunk;
            });
            res.on('end', function () {
                 client_res.writeHead(res.statusCode, res.headers);
                 client_res.end(body);
            });
        });
        req.end();
    });
}
Run Code Online (Sandbox Code Playgroud)

它适用于html页面,但对于其他类型的文件,它只返回一个空白页面或来自目标站点的一些错误消息(在不同的站点中有所不同).

javascript proxy node.js

59
推荐指数
6
解决办法
11万
查看次数

如何使用 ASP.NET Core 解决 REACT 中的 CORS 错误

我有一个 ASP.NET Core Web API 和一个单独的 React 应用程序。Web API 使用 Windows 身份验证。当部署到服务器时,我没有任何问题,但是当我尝试在本地运行应用程序时,我会收到 CORS 错误,并且仅在 POST 操作上出现。这是我的Startup.cs

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using Server.DAL;
using Server.Data;

namespace Server
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<AppDbContext>(options …
Run Code Online (Sandbox Code Playgroud)

cors asp.net-web-api reactjs asp.net-core axios

11
推荐指数
3
解决办法
2万
查看次数

Angular 13:已被 CORS 策略阻止:请求的资源上不存在“Access-Control-Allow-Origin”标头

有人可以帮助我吗?我的 CORS 政策有问题,而且我无法访问网站的后端。

operaGX 导航控制台

这是我在后端(node.js)中使用的代码:

app.use(cors({
  Access_Control_Allow_Origin: "*",
  origin:"*",
  methode:['GET','POST','PATCH','DELETE','PUT'],
  allowedHeaders:'Content-Type, Authorization, Origin, X-Requested-With, Accept'

})); 

Run Code Online (Sandbox Code Playgroud)

这是前端(我使用Angular 13)导入API的代码:environment.ts

export const environment = {
  production: false,
  serverURL: 'http://localhost:3000/api/'
};

Run Code Online (Sandbox Code Playgroud)

我也尝试过这段代码,但它不起作用:

app.use(function (req, res, next) {

    // Website you wish to allow to connect
    res.setHeader('Access-Control-Allow-Origin', 'http://localhost:4200');

    // Request methods you wish to allow
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');

    // Request headers you wish to allow
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');

    // Set to true if you need the website to include cookies in the …
Run Code Online (Sandbox Code Playgroud)

opera swagger-node-express visual-studio-code angular

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