【发布时间】:2020-10-09 09:44:52
【问题描述】:
我想在 React 应用程序中获取请求此链接 https://soundcloud.app.goo.gl/u6XXuqp3Q7Q7WXgH8 的标题。当做一个简单的请求时
request('https://soundcloud.app.goo.gl/u6XXuqp3Q7Q7WXgH8', function (error, response, body) {
console.error('error:', error);
console.log('body:', body);
});
我明白了
Access to fetch at 'https://soundcloud.app.goo.gl/u6XXuqp3Q7Q7WXgH8' from
origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-
Control-Allow-Origin' header is present on the requested resource. If an
opaque response serves your needs, set the request's mode to 'no-cors' to
fetch the resource with CORS disabled.
使用 cors-anywhere 代理时
request('https://cors-anywhere.herokuapp.com/https://soundcloud.app.goo.gl/u6XXuqp3Q7Q7WXgH8', function (error, response, body) {
console.error('error:', error);
console.log('body:', body);
});
我明白了
request.js:150 GET https://cors-anywhere.herokuapp.com/https://soundcloud.app.goo.gl/u6XXuqp3Q7Q7WXgH8 403 (Forbidden)
但是当我只是 cURL 时,我得到了正确的响应
curl -s -o /dev/null -D - https://soundcloud.app.goo.gl/u6XXuqp3Q7Q7WXgH8
谁能向我解释这种行为并告诉我如何克服这个问题?
谢谢!
【问题讨论】:
-
您在此处遇到 CORS 问题,您的客户端域需要在服务器中列入白名单,或者,如果已经列入,则需要更正请求标头。
-
重点是,我无权访问 soundcloud 服务器。并且使用 cors-anywhere 代理适用于除 soundcloud 链接之外的所有其他网站:(
-
我还使用“cors”模块在快速服务器上运行了该应用程序,但没有任何效果:(
标签: javascript node.js reactjs get request