【问题标题】:Webpack dev server Proxy is not working - ReactJSWebpack 开发服务器代理不工作 - ReactJS
【发布时间】:2017-10-25 12:19:23
【问题描述】:

我想在 localhost:3000/upct/ROUTES 上运行我的项目

但我的 API 在:http://desarrollo.com/api

我想在 Webpack 中使用代理选项,但它不起作用。我收到 CORS 和其他错误...我的代理配置如下所示:

    CONFIG.devServer = {
        //host: 'localhost',
        port: 3000,
        proxy: {
            '/api/**': {
                target: 'http://desarrollo.com/api',
                secure: false,
                changeOrigin: true
            }
        },
        contentBase: PATH.join(__dirname, '/src'),
        hot: true,
        inline: true,
        historyApiFallback: true/*,
        headers: { 
            'Content-Type': 'text/plain',
            'Access-Control-Allow-Origin' : '*',
            'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE'
        }*/
    };

我的 AJAX 查询如下:

    $.ajax({
        url: "http://desarrollo.com/api",
        data: "",
        type:"GET",
        dataType: "JSON",
    })
    .done((respuesta) => {
        console.log(respuesta);

    }).fail(function(xhr, textStatus, errorThrown){
        console.log("XHR: ", xhr/*.responseText*/, "Text Status: ", textStatus + '\n' + "Error Thrown: ", errorThrown);
    })

我认为代理是用于在我的 API 中执行 AJAX 查询而不会出现 CORS 错误。但它不起作用。这里有什么问题?

谢谢。

【问题讨论】:

    标签: reactjs webpack webpack-dev-server


    【解决方案1】:

    要向@GProst 响应添加更多内容,以下更改将起作用。

    查找协议:

    this.protocol = () => {
            let returnValue = 'https://';
            if (location.protocol !== 'https:') {
                returnValue = 'http://';
            }
            return returnValue;
    
    };
    

    查找主机名:

    const hostName = window.location.hostname + (!window.location.port === false ? ':' + window.location.port : '');
    

    必填网址:

    const URL = this.protocol() + hostName;
    

    现在上面的 URL 可以在 Ajax 中使用了。

    $.ajax({
        url: URL,
        data: "",
        type:"GET",
        dataType: "JSON",
    })
    

    这将适用于 webpack-dev-server 以及应用程序服务器,例如阿帕奇。

    【讨论】:

    • 我将它添加到我的 webpack 配置文件之上,但我在 npm run serve 上生成了一个错误。谢谢。
    【解决方案2】:

    使用代理时,您必须将请求发送到localhost,以便代理可以将它们重定向到没有 CORS 的远程服务器。在你的$.ajax() 中传递url: "/api"。

    之后,当您在本地运行您的应用程序时,您的请求将发送到http://localhost:3000/api,当它在http://desarrollo.com 上运行时,它会将请求发送到http://desarrollo.com/api。

    【讨论】:

    • 嗨!非常感谢您的回答。我这样做了。我的 API 是用 Laravel + CAS + Apache2 完成的。因为我需要一个 API 子路由 (/api/perfil),所以我做了 AJAX (url='/api/perfil') 但我仍然遇到 CORS 错误:XMLHttpRequest cannot load https://autentica-des.upct.es/cas/login?service=http%3a%2f%2fingeniaticupct.ddns.net%2fapi%2fperfil%2fapi%2fperfil. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. 谢谢。
    • 在您的 webpack 配置中,您显示了 http://desarrollo.com 的代理。但是您现在显示的错误来自https://autentica-des.upct.es。我认为您从本地主机发出的其他请求,没有被代理
    • 是的。 http://desarrollo.com 是托管我的 API。当我打电话给http://desarrollo.com/api/perfil 时,我有一项服务可以连接到 CAS 并返回我的个人数据。
    • 好的,但您似乎直接从客户端而不是代理向 CAS 发出请求。您需要为此域创建另一个代理。我会尽快更新我的答案。
    • 嗯,但在此之前,您能否显示确切的代码,该请求是如何提出的?那个去https://autentica-des.upct.es
    猜你喜欢
    • 2017-12-15
    • 1970-01-01
    • 2022-01-15
    • 2021-01-27
    • 2017-09-12
    • 1970-01-01
    • 2018-07-02
    • 1970-01-01
    • 2020-05-04
    相关资源
    最近更新 更多