部署在heroku上的React应用程序,但api调用不起作用

时间:2018-02-27 19:05:58

标签: reactjs api heroku proxy

我在Heroku上部署的应用程序出现问题!我在后端使用节点制作的API!它在本地工作正常,但是当我部署应用程序时,我有此消息“无效的主机标题”。当我删除我的Package.json中的代理行时,它可以工作,但我自动无法访问我的API!

我的Api部署在另一个dyno中并且工作正常!

的package.json

{
  "name": "app",
  "version": "0.1.0",
  "private": true,
  "proxy": "https://myapp.herokuapp.com/",
  "dependencies": {
    "awesome-typescript-loader": "^3.4.1",
    "axios": "^0.17.1",
    "babel": "^6.23.0",
    "babel-plugin-react-css-modules": "^3.3.2",
    "babel-preset-es2015": "^6.24.1",
    "bootstrap": "^3.3.7",
    "css-loader": "^0.28.8",
    "i": "^0.3.6",
    "jquery": "^3.2.1",
    "jwt-decode": "^2.2.0",
    "prop-types": "^15.6.0",
    "radium": "^0.21.0",
    "react": "^16.2.0",
    "react-big-calendar": "^0.18.0",
    "react-bootstrap": "^0.31.5",
    "react-dom": "^16.2.0",
    "react-redux": "^5.0.6",
    "react-responsive": "^4.0.4",
    "react-router": "^4.2.0",
    "react-router-dom": "^4.2.2",
    "react-scripts": "1.0.17",
    "react-style": "^0.5.5",
    "redux": "^3.7.2",
    "redux-devtools-extension": "^2.13.2",
    "redux-thunk": "^2.2.0",
    "semantic-ui-css": "^2.2.12",
    "semantic-ui-less": "^2.2.12",
    "semantic-ui-react": "^0.77.1",
    "styled-components": "^2.4.0",
    "url-loader": "^0.6.2",
    "validator": "^9.2.0",
    "webpack": "^3.10.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "webpack": "webpack",
    "eject": "react-scripts eject",
    "lint": "eslint src",
    "dev": "webpack-dev-server --devtool eval --progress --colors --content-base build"
  },
  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-plugin-transform-class-properties": "^6.24.1",
    "babel-plugin-transform-object-rest-spread": "^6.26.0",
    "babel-preset-es2016": "^6.24.1",
    "css-loader": "^0.28.8",
    "eslint": "^4.13.1",
    "eslint-config-airbnb": "^15.0.1",
    "eslint-config-prettier": "^2.9.0",
    "eslint-plugin-import": "^2.8.0",
    "eslint-plugin-jsx-a11y": "^5.1.1",
    "eslint-plugin-prettier": "^2.3.1",
    "eslint-plugin-react": "^7.5.1",
    "extract-text-webpack-plugin": "^3.0.2",
    "file-loader": "^1.1.6",
    "img-loader": "^2.0.0",
    "less": "^2.7.3",
    "less-loader": "^4.0.5",
    "node-sass": "^4.7.2",
    "nodemon": "^1.13.3",
    "prettier": "^1.9.2",
    "react-svg-loader": "^2.1.0",
    "resolve-url-loader": "^2.2.1",
    "sass-loader": "^6.0.6",
    "style-loader": "^0.19.1",
    "url-loader": "^0.6.2",
    "webpack": "^3.10.0",
    "webpack-dev-server": "^2.10.1"
  }
}

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

您需要更改代理以匹配您的heroku应用名称:

"proxy": "https://<insert your app name here>.herokuapp.com/",

答案 1 :(得分:-1)

这很有趣,当在我的本地环境中运行应用程序并调用heroku api时,代理设置会起作用

当我部署它时,它不会调用Heroku API