zoukankan      html  css  js  c++  java
  • 求教——使用node做表单,刷新浏览器页面,浏览器为什么会重复提交上次所填的信息

    最近在学些node,按照《nodejs实战》上的代码做练习,发现有表单重复提交的问题

    第一次打开页面,显示如图是get请求

      

           图1

    现在我们提交aaa,显示如图,post请求

      

          图2

    刷新页面,应该显示图1的,结果确实下面这张图,post请求。即使是按ctrl+f5刷新没用,浏览器还是重复提交了上次的内容,显示如图

      

          图3

    不断的刷就这样了,求教这个是为什么?

      

          图4

    node代码如下

    var http=require("http");
    var qs = require("querystring");
    var items=[];
    var server=http.createServer(function(req,res){
        console.log("req.url",req.url);
        console.log("req.method",req.method);
        if("/"==req.url){
            switch(req.method){
                case "GET":
                    show(res);
                    break;
                case "POST":
                    add(req,res);
                    break;
                default:
                    badRequest(res);
            }
        }else{
            notFound(res);
        }
    })
    server.listen(3000);
    function show(res){
        var html='<html><head><title>Todo List</title></head><body>'
            +'<h1>Todo List</h1>'
            +'<ul>'
            +items.map(function(item){
                return '<li>'+item+'</li>'
            }).join('')
            +'</ul>'
            +'<form method="post" action="/">'
            +'<p><input type="text" name="item"/></p>'
            +'<p><input type="submit" value="Add Item"/></p>'
            +'</form></body></html>';
        res.setHeader("Content-Type","text/html");
        res.setHeader("Content-Length",Buffer.byteLength(html));
        res.end(html);
    }
    function notFound(res){
        res.statusCode=404;
        res.setHeader("Content-Type","text/plain");
        res.end("notFound");
    }
    function badRequest(res){
        res.statusCode=400;
        res.setHeader("Content-Type","text/plain");
        res.end("Bad Request");
    }
    function add(req,res){
        var body='';
        req.setEncoding("utf-8");
        req.on("data",function(chunk){
            console.log("chunk",chunk);
            body+=chunk;
        })
        req.on("end",function(){
            var obj=qs.parse(body);
            items.push(obj.item);
            show(res);
        })
    }
  • 相关阅读:
    flex 图表使用百分比示例
    flex 图标设置百分比或者其它符号
    大学生求职(打油诗一首)
    flex 图表categoryField设置 labelFunction使用
    如何配置EclipseMe
    google chart图表使用
    Codeforces #Round 632 div2 A~C
    牛客的两道dfs
    约数
    Atcoder ABC162 D RGB Triplets
  • 原文地址:https://www.cnblogs.com/gg1234/p/5477188.html
Copyright © 2011-2022 走看看