我想在React中显示来自URL的图像.例如,我想要这个图像
https://images.pexels.com/photos/20787/pexels-photo.jpg?auto=compress&cs=tinysrgb&h=350
Run Code Online (Sandbox Code Playgroud)
显示在卡体或reactJS中.是否可以这样做或者我必须先将其下载到资产才能显示它?怎么做?
我已经尝试为我的实习创建一个反应web应用程序几天了,我遇到了CORS错误.我正在使用最新版本的reactJS,并将其放在create-react-app,以及下面是获取的代码:
componentDidMount() {
fetch('--------------------------------------------',{
method: "GET",
headers: {
"access-control-allow-origin" : "*",
"Content-type": "application/json; charset=UTF-8"
}})
.then(results => results.json())
.then(info => {
const results = info.data.map(x => {
return {
id: x.id,
slug: x.slug,
name: x.name,
address_1: x.address_1,
address_2: x.address_2,
city: x.city,
state: x.state,
postal_code: x.postal_code,
country_code: x.country_code,
phone_number: x.phone_number,
}
})
this.setState({warehouses: results, lastPage: info.last_page});
})
.then(console.log(this.state.warehouses))
}
Run Code Online (Sandbox Code Playgroud)
很抱歉,由于公司规则,我无法发布API的网址,但是,确认API后端没有CORS设置.
但是,在mozilla上运行时遇到以下错误
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at ------------------. (Reason: CORS header ‘Access-Control-Allow-Origin’ …Run Code Online (Sandbox Code Playgroud)