【问题标题】:why 500 - internal server error?为什么 500 - 内部服务器错误?
【发布时间】:2018-03-06 14:46:17
【问题描述】:
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 状态代码。为什么这样??可能的原因是什么?

【问题讨论】:

  • 请出示您的后端代码。
  • @Kevin 添加了同样的内容。
  • 我看到您的代码在 module.exports 之前有一个字符“1”。
  • @Kevin 我在该承诺代码中遇到了问题。我猜,Fetch 没有返回任何东西。或者它没有按预期工作。
  • 如果你得到500错误,那应该是你的后端代码有问题。我以前没有使用过 pusher 库。我建议您从后端代码中删除 pusher.trigger,然后简单地发回一些文本以查看它是否有效。如果是的话,你应该看看pusher的文档并把它改正。

标签: javascript node.js express pusher-js


【解决方案1】:

我只需要使用body-parser 包即可。

【讨论】:

    猜你喜欢
    • 2017-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-17
    • 2011-10-17
    • 2010-11-15
    相关资源
    最近更新 更多