Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/421.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么我在尝试发布到表单时会遇到Cannot POST/api/错误?_Javascript_Html_Node.js_Express_Routes - Fatal编程技术网

Javascript 为什么我在尝试发布到表单时会遇到Cannot POST/api/错误?

Javascript 为什么我在尝试发布到表单时会遇到Cannot POST/api/错误?,javascript,html,node.js,express,routes,Javascript,Html,Node.js,Express,Routes,我有一个基本的网站,遇到了一个问题。我对这一点非常陌生,似乎无法解决它 当我运行服务器并浏览到http://localhost:3000/class/create 一切正常。然而,当我尝试添加学生时,我在浏览器上收到一条消息,上面写着 控制台中出现“无法POST/api/create”和404错误 应该发生什么:控制台记录“创建学生条目” index.js classRoutes.js create.html 类列表:创建 输入学生的姓名: 添加 Chrome开发工具输出(网络选项卡) 因为

我有一个基本的网站,遇到了一个问题。我对这一点非常陌生,似乎无法解决它

当我运行服务器并浏览到http://localhost:3000/class/create 一切正常。然而,当我尝试添加学生时,我在浏览器上收到一条消息,上面写着

控制台中出现“无法POST/api/create”和404错误

应该发生什么:控制台记录“创建学生条目”

index.js classRoutes.js create.html

类列表:创建
输入学生的姓名:
添加
Chrome开发工具输出(网络选项卡)
因为您设置了
app.use('/class',classRouter)
/class
必须在
/api/create
之前,因此正确的路径是
/class/api/create


使用正确的绝对路径:
action=“/class/api/create”

没有response@NicolasI抱歉?当您执行请求时,Chrome开发工具中的“网络”选项卡会显示什么?@zishone我在尝试请求时已附加了开发工具输出,因为该位置没有路由。我尝试了此操作,但仍然不起作用
const express = require('express')

const app = express()

// loading body-parser
const bodyParser = require('body-parser')

// loading our routers
const mainRouter = require('./mainRoutes.js')
const classRouter = require('./classRoutes.js')

// tell Express to use bodyParser for JSON and URL encoded form bodies
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))

// mounting the routers
app.use('/', mainRouter)
app.use('/class', classRouter)

app.listen(3000)
console.log('Express server running on port 3000')

const path = require('path')
const express = require('express')
const router = express.Router()

const classList = [] // our class list array

router.get('/', function (req, res) {
  res.sendFile(path.join(__dirname, 'views', 'class', 'index.html'))
})

router.get('/create', function (req, res) {
  res.sendFile(path.join(__dirname, 'views', 'class', 'create.html'))
})

// RESTful api
router.get('/api/list', function (req, res) {
  res.json(classList) // Respond with JSON
})

router.get('/api/get/:id', function (req, res) {
  res.json(classList[req.params.id]) // Notice the wildcard in the URL?
  // Try browsing to /api/get/0 once you've added some entries
})


router.post('/api/create', function (req, res) {
  console.log('creating a student entry')
})

module.exports = router

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Class List: Create</title>
    <meta charset="utf-8" />
</head>
<body>
    <form action="/api/create" method="post">
    <div>
        <label for="studentName">Enter The Student's Name:</label>
        <input type="text" id="student" name="student">
    </div>
    <div class="button">
        <button type="submit">Add</button>
    </div>
    </form>
</body>
</html>