【问题标题】:Cannot Post to localhost "POST http://localhost:8000/sendpage 404 (Not Found)" in console无法在控制台中发布到本地主机“POST http://localhost:8000/sendpage 404(未找到)”
【发布时间】:2020-08-09 19:48:42
【问题描述】:

我无法将任何内容发布到我的 localhost/sendpage,这是一个 json 文件。 虽然我可以从中得到。为什么?控制台错误是“POST http://localhost:8000/sendpage 404 (Not Found)”

服务器:

var express = require('express');
var app = express();
var http = require('http').Server(app);
var path = require('path');

var port=8000;
app.set('port',(process.env.PORT || port));

// Serve static assets from public/
app.use(express.static(path.join(__dirname, 'public/')));

var server = http.listen(app.get('port'), function () {
  console.log('Server listening on port ' + app.get('port'));
});


app.get('/', function (req, res) {
  res.sendFile(path.join(__dirname, 'public/index.html'));
});

app.get('/sendpage', function(req,res){
  res.json({
"id":40000,
"body":"Name: Vera\nEmail: vera.wid@gmail.com\n",
"read":true,"secret":false,
"direction":"in",
"readAt":"2020-04-24T13:45:41Z",
"createdAt":"2020-04-24T13:45:13Z",
"updatedAt":"2020-04-24T13:45:41Z",
"ChatWebsiteId":1,
"ChatInteractionId":249,
"UserId":11,
"ContactId":11584,
"AttachmentId":null,
"User":{"transport":null,"nat":null,"allow":null,"insecure":null,"permissions":[],"phoneBarEnableVideoRecording":false,"id":11,"fullname":"Bruce Wayne","alias":null}
})



});

连接到按钮的javascript函数:

function addMessage() {

  axios.post('http://localhost:8000/sendpage', {
  "id":40000,
  "body":"Name: Vera\nEmail: vera.wid@gmail.com\n",
  "read":true,"secret":false,
  "direction":"in",
  "readAt":"2020-04-24T13:45:41Z",
  "createdAt":"2020-04-24T13:45:13Z",
  "updatedAt":"2020-04-24T13:45:41Z",
  "ChatWebsiteId":1,
  "ChatInteractionId":249,
  "UserId":11,
  "ContactId":11584,
  "AttachmentId":null,
  "User":{"transport":null,"nat":null,"allow":null,"insecure":null,"permissions":[],"phoneBarEnableVideoRecording":false,"id":11,"fullname":"Bruce Wayne","alias":null}



})




.then(res=>console.log(res))
.catch(err=>console.error(err))
}

为什么当我用 get 替换 POST 时,它会起作用?

【问题讨论】:

    标签: javascript node.js post get axios


    【解决方案1】:

    您没有像使用 GET 那样在快速代码中处理 POST 请求

    app.get('/sendpage', function(req,res){
      res.json({
      "id":40000,
      "body":"Name: Vera\nEmail: vera.wid@gmail.com\n",
      "read":true,"secret":false,
      "direction":"in",
      "readAt":"2020-04-24T13:45:41Z",
      "createdAt":"2020-04-24T13:45:13Z",
      "updatedAt":"2020-04-24T13:45:41Z",
      "ChatWebsiteId":1,
      "ChatInteractionId":249,
      "UserId":11,
      "ContactId":11584,
      "AttachmentId":null,
      "User":{"transport":null,"nat":null,"allow":null,"insecure":null,"permissions":[],"phoneBarEnableVideoRecording":false,"id":11,"fullname":"Bruce Wayne","alias":null}
    })
    

    可以如下添加

    app.post('/sendpage',function(req,res){
      //do something
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-01
      • 1970-01-01
      • 2021-10-14
      • 2021-06-19
      • 1970-01-01
      • 2016-08-06
      • 2020-07-23
      • 1970-01-01
      相关资源
      最近更新 更多