admin管理员组文章数量:1356413
I have a node.js express application which is trying to make a post request and query the contents of a database. I am trying to execute the post request without have to redirect the user from the form page or reloading the page.
index.jade
form.well(id="newEntryForm", method="post", action="/addEntry")
label Key:
br
input(name="key", id="key", class="new-entry", type="textbox")
br
br
label Value:
br
input(name="value", id="value", class="new-entry", type="textbox")
br
br
button.btn(type="submit") Add Entry
app.js
app.post('/addEntry', function(req, res){
//database query
});
This ends up redirecting me to the URL localhost:3000/addEntry
. I know I can add res.redirect("/");
within the callback for the post method, but that will reload the page. How do I acplish this without reloading the page?
EDIT
I added a javascript method to execute on submitting the form, but it ends up appending the POST request parameters on the URL bar. How do I avoid this?
Javascript method
$(function() {
$('#newEntryForm').submit(function() {
var data = {key: 'a', value: 'b'};
// build a json object or do something with the form, store in data
$.post('/addRule', data, function(resp) {
alert(resp);
console.log("post response: " + resp);
// do something when it was successful
});
});
});
I have a node.js express application which is trying to make a post request and query the contents of a database. I am trying to execute the post request without have to redirect the user from the form page or reloading the page.
index.jade
form.well(id="newEntryForm", method="post", action="/addEntry")
label Key:
br
input(name="key", id="key", class="new-entry", type="textbox")
br
br
label Value:
br
input(name="value", id="value", class="new-entry", type="textbox")
br
br
button.btn(type="submit") Add Entry
app.js
app.post('/addEntry', function(req, res){
//database query
});
This ends up redirecting me to the URL localhost:3000/addEntry
. I know I can add res.redirect("/");
within the callback for the post method, but that will reload the page. How do I acplish this without reloading the page?
EDIT
I added a javascript method to execute on submitting the form, but it ends up appending the POST request parameters on the URL bar. How do I avoid this?
Javascript method
$(function() {
$('#newEntryForm').submit(function() {
var data = {key: 'a', value: 'b'};
// build a json object or do something with the form, store in data
$.post('/addRule', data, function(resp) {
alert(resp);
console.log("post response: " + resp);
// do something when it was successful
});
});
});
Share
Improve this question
edited Jun 12, 2014 at 20:40
RagHaven
asked Jun 12, 2014 at 20:07
RagHavenRagHaven
4,34725 gold badges75 silver badges114 bronze badges
2
- You need to use client-side Javascript and AJAX. – SLaks Commented Jun 12, 2014 at 20:07
- I tried doing so. I had some trouble executing the javascript method on submitting the form, also when I did get it to submit, the post request information was appended to the URL. Could you provide an example? – RagHaven Commented Jun 12, 2014 at 20:12
1 Answer
Reset to default 5As a ment said, you need to use client side javascript+ajax.
$(function() {
$('#newEntryForm').submit(function(event) {
event.preventDefault(); // Stops browser from navigating away from page
var data;
// build a json object or do something with the form, store in data
$.post('/addEntry', data, function(resp) {
alert(resp);
// do something when it was successful
});
});
});
本文标签: javascriptnodejsexpress making post request without redirecting from current pageStack Overflow
版权声明:本文标题:javascript - node.jsexpress making post request without redirecting from current page - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744065654a2584942.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论