简体   繁体   English

如何从ReactJS中的任何URL获取元数据(图像,标题,描述)

[英]How to fetch metadata (image, title, description) from any url in ReactJS

I want to fetch metadata (title, image, description) from a url like a Medium article and then display it as a post on my blog. 我想从网址(如中型文章)中获取元数据(标题,图像,描述),然后将其显示为我的博客上的帖子。 How to do it. 怎么做。 I am using ReactJS 我正在使用ReactJS

I tried using fetch and axios.get but it always shows error in the browser's console. 我尝试使用fetch和axios.get,但它始终在浏览器的控制台中显示错误。 "NetworkError when attempting to fetch resource." “尝试获取资源时出现NetworkError。” "Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://medium.com/s/jessica-valenti/hateful-fox-news-rhetoric-can-do-real-world-harm-52e26008caa5 . (Reason: CORS header 'Access-Control-Allow-Origin' missing)" “跨原始请求被阻止:“同一来源策略”不允许在https://medium.com/s/jessica-valenti/hateful-fox-news-rhetoric-can-do-real-world-harm-52e26008caa5读取远程资源(原因:CORS标头“ Access-Control-Allow-Origin”丢失)”

Use open-graph-scraper and make the request from backend-server. 使用open-graph-scraper并从后端服务器发出请求。 It works like a delight 它像一种喜悦

You are not allowed to request request URL's that don't have the CORS header ( Access-Control-Allow-Origin: * ). 不允许您请求没有CORS标头的请求URL( Access-Control-Allow-Origin: * )。 This is due security reasons of medium and other pages. 这是由于中等和其他页面的安全性原因。 They need to actively enable that header to allow clients to request it from JS. 他们需要主动启用该标头,以允许客户端从JS请求它。

You will need a backend that does the request for you (nodejs, python, php etc). 您将需要一个为您做请求的后端(nodejs,python,php等)。 You could also try to use https://cors-anywhere.herokuapp.com . 您也可以尝试使用https://cors-anywhere.herokuapp.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM