0
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 状态代码。为什么这样??可能的原因是什么?

4

1 回答 1

0

我只需要使用body-parser相同的包。

于 2018-03-06T06:46:17.983 回答