将 POST 获取到 localhost:3000 而不是 localhost:8080

时间:2021-01-15 14:03:15

标签: javascript reactjs fetch package.json

我已将 "proxy": "http://localhost:8080" 添加到我的 package.json 文件中,以便它将 POST 定向到 8080,但它显示 AddBook.js:34 POST http://localhost:3000/api/book 404 (Not Found)

为什么要发送到 3000?

{
  "name": "appone-fe",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@material-ui/core": "^4.11.2",
    "@material-ui/icons": "^4.11.2",
    "@testing-library/jest-dom": "^5.11.4",
    "@testing-library/react": "^11.1.0",
    "@testing-library/user-event": "^12.1.10",
    "react": "^17.0.1",
    "react-dom": "^17.0.1",
    "react-scripts": "4.0.1",
    "web-vitals": "^0.2.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "proxy": "http://localhost:8080",
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

这是我的 handlesubmit 函数,我也试过只发布到“/book”

    const handleSubmit = (event) => {
        event.preventDefault()
        fetch("/api/book", {
            method: "POST",
            headers: {"Access-Control-Allow-Origin": "*"},
            body: {
                ...state
            }
        }).then((a) => {
            alert(`status code ===> ${a.status}`)
        }).catch(error => alert(error))
    }

0 个答案:

没有答案
相关问题