admin管理员组文章数量:1428984
I want to build a small website. I have backend almost done but i can't find how can i send form data to server. Here is my attempt and i want to send the data to localhost running at port 3000 for example:
jQuery('#signUp-Form').on('submit',function(e){
e.preventDefault();
console.log('Sending data');
var name = jQuery('[name = username]').val();
// socket.emit('sign-up',{
// username: jQuery('[name = username]').val(),
// password: jQuery('[name = password]').val()
// });
});
I can do with socket.io but however i want to use POST,GET etc
and at server side i built my server with express.
I can retrieve the name from the field into the var name
and now i want to send it to server: here is the function which will handle this request. Only first two lines:
app.post('/login',(req,res) => {
var body = _.pick(req.body, ['username', 'password']);
Now my question is how can i send data in such format so that '/login' matches with pattern and req.body
contains the name only.
It is the basic question but i don't see anywhere how can i do this.
I want to build a small website. I have backend almost done but i can't find how can i send form data to server. Here is my attempt and i want to send the data to localhost running at port 3000 for example:
jQuery('#signUp-Form').on('submit',function(e){
e.preventDefault();
console.log('Sending data');
var name = jQuery('[name = username]').val();
// socket.emit('sign-up',{
// username: jQuery('[name = username]').val(),
// password: jQuery('[name = password]').val()
// });
});
I can do with socket.io but however i want to use POST,GET etc
and at server side i built my server with express.
I can retrieve the name from the field into the var name
and now i want to send it to server: here is the function which will handle this request. Only first two lines:
app.post('/login',(req,res) => {
var body = _.pick(req.body, ['username', 'password']);
Now my question is how can i send data in such format so that '/login' matches with pattern and req.body
contains the name only.
It is the basic question but i don't see anywhere how can i do this.
Share Improve this question asked Oct 31, 2018 at 17:33 Khan SaabKhan Saab 5111 gold badge8 silver badges21 bronze badges 01 Answer
Reset to default 3Khan, it looks like you are attempting two methods of form submission: non-AJAX and AJAX. From your question it's not apparent which method is intended, but I will show both methods:
Non-AJAX
Using <form action="/login" method="POST">
:
<form id="myform" action="/login" method="POST">
<input type="text" name="username">
<input type="password" name="password">
<button type="submit">login</button>
</form>
This method will refresh the page and make a POST request to /login sending the url encoded data like:
username=nha&password=asonteu
AJAX
With AJAX, the page won't refresh, and you must send the data programmatically:
$( "#myform" ).on( "submit", function( event ) {
event.preventDefault();
let data = $( this ).serialize();
//make our request here
$.post( "/login", function( data ) {
console.log(data);
});
});
serialize()
also url encodes the form data.
Your server must handle any expected data format (to extract the fields properly). URL encoding is useful, but later you will see JSON or perhaps protobufs. It really doesn't matter which format you use as long as both the client and server agree.
NOTE: With POST, the data will be in the request body
I hope this helps!
本文标签: javascriptHow to send form data to serverStack Overflow
版权声明:本文标题:javascript - How to send form data to server - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745446857a2658701.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论