1

使用 jquery post 方法向节点 js 服务器发送表单时,URL 中出现参数。

我编写了 app.js 代码如下

          var express = require('express'),
          app = express(),
          server = require('http').createServer(app),

           var bodyParser = require('body-parser');

            app.use(bodyParser.urlencoded({
                 extended: true
             })); 
          app.use(bodyParser.json());

           app.post('/Send', function(req, res){

                 var userName = req.body.Name;
                 var city = req.body.Cname;
                 var pass = req.body.pass; 

           });

index.html 为

          <form id="registration" >
           <input type="text" name="user_name" />
           <input type="password" name="pass" />
           <input type="submit" id="button" value="Login" />  
           </form>

           <script>
           $('#button').click(function(){
           $.post( "/Send", $( "#registration" ).serialize());
           }); 
           </script>

参数出现在 URL 中如何隐藏它们。

4

2 回答 2

2

$.post 不应该对您的 URL 做任何事情。演示

确保您正在阻止默认表单行为并且您已准备好 DOM

<form id="registration" >
    <input type="text" name="user_name" />
    <input type="password" name="pass" />
    <input type="submit" id="button" value="Login" />  
</form>

<script>
$(document).ready(function(){
    $('#button').click(function(e){
        e.preventDefault();
        // why capitalize "/Send"? also change route on back-end
        $.post( "/send", $( "#registration" ).serialize(), function(data) {
            // success
        });
    }); 
});
</script>
于 2014-12-11T06:02:59.940 回答
0

您应该向$.post.
尝试跟随:

function getFormData(id) {
    var data = {};
    var inputs = $('#'+id).serializeArray();
    $.each(inputs, function (i, input) {
        data[input.name] = input.value;
    });
    return data;
}
于 2014-12-11T05:54:06.917 回答