最近做一个React的简单项目,需要调用WebApi接口,调用遇到了CORS跨域问题,特此记录一下:
简单的js页面如下:
import React, { Component } from 'react'; class App extends Component{ render(){ return( <ShoppingList name="Mark" /> ); } }; var REQUEST_URL = "https://localhost:44359/api/values"; class ShoppingList extends React.Component { constructor(props){ super(props); this.state={ error:null, isLoaded:false, items:[] } } componentDidMount(){ fetch(REQUEST_URL, { method: "GET", // *GET, POST, PUT, DELETE, etc. mode: 'cors' // no-cors, cors, *same-origin }) .then(res=>res.json()) .then( (result)=>{ this.setState({ isLoaded:true, items:result.items }); }, (error)=>