鍍金池/ 問答/人工智能  HTML5  Linux  HTML/ create-react-app創(chuàng)建的react項(xiàng)目build后使用github

create-react-app創(chuàng)建的react項(xiàng)目build后使用github pages后跨域設(shè)置失效

package.json里這樣設(shè)置,使用fetch請(qǐng)求,開發(fā)時(shí)沒問題

"proxy": {
  "/api": {
    "target": "https://h5.ele.me",
    "changeOrigin": true,
    "pathRewrite": {
      "^/api": "/"
    }
  }
}
// 請(qǐng)求
fetch(`api/restapi/shopping/v3/hot_search_words?latitude=${a.latitude}&longitude=${a.longitude}`)

build 后使用 github pages 部署,請(qǐng)求出錯(cuò),下面紅框里的那段應(yīng)該是上面的 target,所以請(qǐng)求失敗了,這種情況該如何修改呢,不能的話有別的方法預(yù)覽項(xiàng)目嗎

clipboard.png

回答
編輯回答
薔薇花

新建一個(gè)nginx配置文件gihtubpage.conf

server {
    listen 80 default_server;
    listen [::]:80 ipv6only=on default_server;
    location /api/ {
        proxy_pass https://h5.ele.me/;
    }
}
2017年5月22日 15:56