显示错误“无法发布”当我尝试提交HTML表单时 [英] Shows error "Cannot POST" when I try to submit the HTML form

查看:143
本文介绍了显示错误“无法发布”当我尝试提交HTML表单时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过同样错误的其他问题,但没有一个答案似乎有效。

I have seen other questions with the same error, but none of the answers seem to work.

<!DOCTYPE html>
<html>
<body>

<form action="http://127.0.0.1:8080/del" method="post">
  First name: <input type="text" name="fname"><br>
  Last name: <input type="text" name="lname"><br>
  <input type="submit" value="Submit">
</form>

<p>Click on the submit button, and the input will be sent to a page on the server called "http://127.0.0.1:8080/del".</p>

</body>
</html>

server.js

var express=require('express');
var body_parser=require('body-parser');
var request = require('request').defaults({json:true});
var app=express();
var del=require('./del'); 
app.post('./del',del.test);
var server = app.listen(8080,function(){
    var host="127.0.0.1";
    var port="8080";
    console.log("App is listening at http://%s:%s\n",host,port);
});

del.js

module.exports={
    test: function(){
        console.log("Hello world.");
    }
};

每次提交表格时,都会显示

Each time, when I submit the form, it shows


不能POST / del

Cannot POST /del


推荐答案

In您的 server.js 更改此行:

app.post('./del',del.test);

到此:

app.post('/del',del.test);

然后你有正确的路由器。

then you have correct router.

您的 del.js 更改为:

module.exports={
    test: function(req, res){
        console.log("Hello world.");
        res.status(200).end();
    }
};

因为路由器功能应该返回响应。

because router function should return response.

这篇关于显示错误“无法发布”当我尝试提交HTML表单时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆