【问题标题】:Getting the HTML source of a page in NodeJS在 NodeJS 中获取页面的 HTML 源代码
【发布时间】:2018-01-05 04:41:10
【问题描述】:

由于 cors 限制,我目前正在努力在 NodeJS 中获取网页的 HTML 源代码。使用 JQuery 我正在向期望 HTML 响应的页面发出 GET 请求。然而,这会引发错误XMLHttpRequest cannot load https://www.reddit.com/. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:8000' is therefore not allowed access。有没有办法使用 NodeJS 绕过 CORS? (比如通过代理)

        $.ajax({
        url: 'https://www.reddit.com/',
        headers: {
            'Content-Type': 'text/html',
            "Access-Control-Allow-Origin": '*'
        },
        type: "GET",
        data: {
        },
        success: function (result) {
            console.log(result);    
        },
        error: function () {
            console.log("error");
        }
    });

我也在使用带有以下标头的 webpack 开发服务器

    headers: {
        "Access-Control-Allow-Origin": '*',
        "Access-Control-Allow-Credentials": "true",
        "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS"
    }

【问题讨论】:

标签: javascript jquery node.js webpack


【解决方案1】:

不要在 nodejs 中使用 jquery,你可以使用这个库来归档你需要的东西:

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-24
    • 2017-06-06
    • 2012-07-22
    • 1970-01-01
    相关资源
    最近更新 更多