我不能向我的localhostsendpage发布任何东西,这是一个json文件。虽然我可以从它那里GET。为什么会这样?控制台的错误是 "POST http:/localhost:8000sendpage。 404(未找到)"
服务器。
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: [email protected]\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: [email protected]\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))
}
为什么当我把POST替换成get的时候就能用了?
你没有像处理GET那样在你的快递代码中处理POST请求。
app.get('/sendpage', function(req,res){
res.json({
"id":40000,
"body":"Name: Vera\nEmail: [email protected]\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
})