鍍金池/ 問答/HTML5  Linux  HTML/ create-react-app使用fetch跨域?

create-react-app使用fetch跨域?

使用 create-app-react 創(chuàng)建的 react 項目,使用 fetch 請求餓了么的接口,package.json 里已經(jīng)配置了跨域設(shè)置,請求的時候沒效果

"proxy":{
    "/api":{
      "target":"https://h5.ele.me",
      "changeOrigin":true
    }
  }
componentDidMount(){
    fetch('/api/restapi/shopping/openapi/entries?latitude=34.04347829999999&longitude=-118.2519314&templates[]=main_template&templates[]=favourable_template&templates[]=svip_template&terminal=h5').then(response => {
      console.log(response);
      return response;
    })
  }

clipboard.png

回答
編輯回答
舊時光
"proxy":{
    "/api":{
      "target":"https://h5.ele.me",
      "changeOrigin":true,
      "pathRewrite": {
           "^/api": "/"
        }
    }
  }
2017年12月18日 04:38