When sending a form to node js server using jquery post method parameters are appearing in URL.
使用jquery post方法参数将表单发送到节点js服务器时,会出现在URL中。
I had written app.js code as follows
我写了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 as
<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>
Parameters are appearing in URL how to hide them.
参数出现在URL中如何隐藏它们。
2 个解决方案
#1
2
$.post shouldn't do anything to your URL. DEMO
$ .post不应对您的网址执行任何操作。 DEMO
make sure you're preventing default form behavior and that you're DOM is ready
确保您正在阻止默认表单行为,并确保您已准备好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>
#2
0
You should provide object to $.post
.
Try followed:
你应该提供$ .post的对象。尝试遵循:
function getFormData(id) {
var data = {};
var inputs = $('#'+id).serializeArray();
$.each(inputs, function (i, input) {
data[input.name] = input.value;
});
return data;
}
#1
2
$.post shouldn't do anything to your URL. DEMO
$ .post不应对您的网址执行任何操作。 DEMO
make sure you're preventing default form behavior and that you're DOM is ready
确保您正在阻止默认表单行为,并确保您已准备好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>
#2
0
You should provide object to $.post
.
Try followed:
你应该提供$ .post的对象。尝试遵循:
function getFormData(id) {
var data = {};
var inputs = $('#'+id).serializeArray();
$.each(inputs, function (i, input) {
data[input.name] = input.value;
});
return data;
}