对 glitch.me 服务器的跨源请求
Cross-origin request to glitch.me server
我在 glitch.me 上创建了一个服务器,我正在尝试从该服务器提供数据,但出现了以下错误。
本地主机/:1 对“https://clem-quote-server.glitch.me/quotes' from origin 'http://localhost:3000”处的提取访问已被 CORS 策略阻止:
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.
不太确定如何解决这个问题
import React, {Component} from "react"
class quotes extends Component {
constructor(props) {
super(props);
this.state = {
error: null,
isLoaded: false,
quotes: quotes
};
}
componentDidMount() {
fetch("https://clem-quote-server.glitch.me/quotes")
.then(res => res.json())
.then(
(result) => {
this.setState({
isLoaded: true,
quotes: result.quotes
});
},
error => {
this.setState({
isLoaded: true,
error
});
}
);
}
render() {
const { error, isLoaded, quotes } = this.state;
if (error) {
return <div>Error: {error.message}</div>;
} else if (!isLoaded) {
return <div>Loading...</div>;
} else {
return (
<ul>
{quotes.map(quote => {
return quote;
}
)}
</ul>
);
}
}
}
export default quotes;
我希望每次加载页面时都能显示数组列表中的一个对象
由于 CORS 而失败,当服务器和客户端处理不同的域时必须包含它。
CORS 包含在 headers。
以下是如何在您的 React 应用中启用 corse:
https://facebook.github.io/create-react-app/docs/proxying-api-requests-in-development
CORS 信息:
我在 glitch.me 上创建了一个服务器,我正在尝试从该服务器提供数据,但出现了以下错误。 本地主机/:1 对“https://clem-quote-server.glitch.me/quotes' from origin 'http://localhost:3000”处的提取访问已被 CORS 策略阻止:
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.
不太确定如何解决这个问题
import React, {Component} from "react"
class quotes extends Component {
constructor(props) {
super(props);
this.state = {
error: null,
isLoaded: false,
quotes: quotes
};
}
componentDidMount() {
fetch("https://clem-quote-server.glitch.me/quotes")
.then(res => res.json())
.then(
(result) => {
this.setState({
isLoaded: true,
quotes: result.quotes
});
},
error => {
this.setState({
isLoaded: true,
error
});
}
);
}
render() {
const { error, isLoaded, quotes } = this.state;
if (error) {
return <div>Error: {error.message}</div>;
} else if (!isLoaded) {
return <div>Loading...</div>;
} else {
return (
<ul>
{quotes.map(quote => {
return quote;
}
)}
</ul>
);
}
}
}
export default quotes;
我希望每次加载页面时都能显示数组列表中的一个对象
由于 CORS 而失败,当服务器和客户端处理不同的域时必须包含它。
CORS 包含在 headers。
以下是如何在您的 React 应用中启用 corse:
https://facebook.github.io/create-react-app/docs/proxying-api-requests-in-development
CORS 信息: