问题描述
Medium 在 https://medium.com/feed/[@username] 上有一个 RSS 提要].我正在尝试使用 XMLHTTPRequest 获取我的所有博客文章.当我在本地测试时,我遇到了 CORs 错误.当我打开 CORs Chrome 扩展程序时,我收到 401 错误.有任何想法吗?有人成功调用 Medium RSS 了吗?
Medium has an RSS feed available at https://medium.com/feed/[@username]. I'm trying to fetch all my blog posts using an XMLHTTPRequest. When I test on local, I run into CORs errors. When I turn on CORs Chrome extension, I get a 401 error. Any ideas? Has anyone succeeded in calling Medium RSS?
推荐答案
要使用 XHR 获取 https://medium.com/feed/[@username]
内容,您可以发出 XHR 请求通过某种代理.例如,尝试为您当前的 XHR 代码提供此 URL:
To get https://medium.com/feed/[@username]
content using XHR, you can make the XHR request through a proxy of some kind. For example, trying giving your current XHR code this URL:
https://cors-anywhere.herokuapp.com/https://medium.com/feed/@sideshowbarker
这将导致请求转到 https://cors-anywhere.herokuapp.com
,这是一个开放/公共 CORS 代理,然后将请求发送到 https://medium.com/feed/@sideshowbarker
.
That’ll cause the request to go to https://cors-anywhere.herokuapp.com
, a open/public CORS proxy which then sends the request on to https://medium.com/feed/@sideshowbarker
.
当该代理收到响应时,它会接受它并向其添加 Access-Control-Allow-Origin
响应标头,然后将其作为响应传递回您的请求前端代码.
And when that proxy gets the response, it takes it and adds the Access-Control-Allow-Origin
response header to it and then passes that back to your requesting frontend code as the response.
带有 Access-Control-Allow-Origin
响应标头的响应是浏览器看到的,因此浏览器现在向您显示的错误消息消失了,浏览器允许您的前端 JavaScript 代码访问响应.
That response with the Access-Control-Allow-Origin
response header is what the browser sees, so the error message the browser is showing you now goes away, and the browser allows your frontend JavaScript code to access the response.
或使用 https://github.com/Rob--W/中的代码cors-anywhere/ 等来设置您自己的代理.
Or use the code from https://github.com/Rob--W/cors-anywhere/ or such to set up your own proxy.
您需要代理的原因是,来自 https://medium.com/feed/[@username]
的响应不包含 Access-Control-Allow-Origin
响应标头,因此您的浏览器将拒绝让您的前端 JavaScript 代码跨域访问这些响应.
The reason you need a proxy is, responses from https://medium.com/feed/[@username]
don’t include the Access-Control-Allow-Origin
response header, so your browser will refuse to let your frontend JavaScript code access those responses cross-origin.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS 有更多详细信息.
这篇关于如何调用媒体 RSS 提要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!