const form = document.getElementById('vote-form')
form.addEventListener('submit',e => {
let choice = document.querySelector('input[name=os]:checked').value;
let data = {os: choice};
fetch("http://localhost:3000/poll",{ // *** getting error here
method:'post',
body:JSON.stringify(data),
headers: new Headers({
'Content-Type':'application/json'
})
}).then(res => res.json()).then((data) => {console.log(data)}).catch(err => console.log(err))
e.preventDefault();
});
这是后端代码:它是我的主路由器文件,其中放置了所有相关路由
const express = require('express')
const router = express.Router()
const Pusher = require('pusher')
let pusher=new Pusher({
appId:'*******',
key:'*****',
secret:'******',
cluster:'*****',
encrypted:true
});
router.get('/',(req,res) => { //it actually means /poll (as mentioned in app.js)
res.send('polling starts');
})
router.post('/',(req,res)=>{
pusher.trigger('os-poll','os-vote',{
points: 1,
os: req.body.os
});
res.json({success:true,message:"thanks for voting"})
});
module.exports=router
我收到内部服务器错误 - 500状态代码。为什么这样??可能的原因是什么?
答案 0 :(得分:0)
我只需要使用body-parser
包。