How to pass data from script.js to express.js [duplicate] - javascript

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 3 months ago.
This is a basic registration form.
script.js performs input checking.
express.js sends and receives data from mongodb.
What I need to do is send input values (such as username.value) after input checking from script.js to express.js. How do I do it?
things to keep in mind this is an assignment and I cannot use jQuery, React etc. just HTML, CSS, JS, Express and mongodb

You could use the fetch API to do a POST request and capture it with express.
The FormData API works well for getting the data out of a form.
Example:
fetch(/* POST endpoint */, {
body: new FormData(/* form element */),
method: 'POST'
});

Related

Vue sending email [duplicate]

This question already has answers here:
How to send an email from JavaScript
(20 answers)
Closed 4 years ago.
I have a problem. I want to put a contact form on the page, but I do not know how to do it in the Vue. I wanted to do it normally as jquery, php, but it does not work in Vue. Does anyone have an idea what I can do?
Vue is a View (hence the name) library, meant for describing your View layer i.e. how your application looks. Such logic as sending an email would for one be dependent on Javascript itself and not on Vue.
However it is not possible to send an email from Javascript in the browser. Instead you'd need to build a backend which can send an email for you, or you could call the user's email client, both described here.
You would ideally send email data (to, message body - html escaped of course) as a request to a backend end point and send from there. You can use Vue's HTTP resource to send the request: https://github.com/pagekit/vue-resource
But sending an email directly from Vue isn't possible.

Why should I use POST etc methods instead of URL parameters? [duplicate]

This question already has answers here:
When do you use POST and when do you use GET?
(27 answers)
Closed 6 years ago.
We can accomplish the same thing by doing:
localhost:3000/endpoint?var1=val1&var2=val2
That we can do by using POST with a JSON body.
So why should anyone use PUT/POST/PATCH if they can get the same goals by using url params? Even with auth, instead of header, you can send the information of the auth token back and forth by using a parameter?
one reason is that GET parameters have to be URL-encoded.
then there are limitations of URL length documented in RFC I think.
This will make it difficult when transfering large data (e.g. file uploads,...)
additionally, developers may want to hide some informations from the user, to prevent users to bookmark a page with all these parameters...
Definitely NO reason for POST args is security on the transport layer. in both cases the data are plain text in HTTP, and both (also the URL) are encoded end-to-end when using a secure HTTPS connection.
It is more secure, because your data is encrypted and sent in the header of the request.

jQuery AJAX not sending hashtag symbols which causes my request to fail [duplicate]

This question already has answers here:
Passing "#" hash symbol in request parameter of url not working in Firefox
(3 answers)
Closed 6 years ago.
I have a problem. I'm working with Elixir Phoenix and React.JS. I have a Token that is used to verify the user. This Token also has hashtags in it so when I send a request to verify it, it is being sent without the hash symbols and what comes after them which makes my request to fail. How can I solve this issue? For some reason I don't have that problem when I use fetch with React Native, but here I do. I tried many variations as you can see in the image.
The hash and anything after it will not be sent by browsers in a request.
You should encode the token so that the hash is escaped.
fetch('http://localhost:4000/api/users/'+encodeURIComponent(accesstoken), {

How to save a JS variable value on PHP site? [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 8 years ago.
I'm writing a little online programm that must save a football tournament results. I'm adding a team names using JS, and i want to save them on server site (in text file). For this i need PHP, i think. JS looks like this:
<script>
function addTeam(){
var name = $("#FormForNameEntering").val();
if (name != ""){
$("#TableDataNameContainer").append("<p class='teamName'>" + name + "</p>");
$('#FormForNameEntering').val("");
}
}
</script>
So i need to save variable "name" in text file. For this i need to tranfer its value to PHP? How?
You have three options. You would make an Ajax call, form submission of some kind with a submit button and action to be set to a php file, or finally have your whole page in a php file and after receiving data in JavaScript do your php there.
You need to send it to the server! e.g:
window.location = www.example.com?variableName='+yourJSVariable
And just retrieve it via PHP's $_GET. Other option you have is sending it to the server asynchronously (=in background lets say). Have a look at jQuery's ajax/get/post functions, or play around with XHR obj in js. Or try submiting via html forms.
Dont worry we've all started somewhere!

How log express.js POST parameters [duplicate]

This question already has answers here:
How to process POST data in Node.js?
(30 answers)
Closed 8 years ago.
I'm wondering how to get an array that contains all parameters sent in a HTTP request.
If I read the doc: http://expressjs.com/4x/api.html#req.params
I can see that using req.params and req.query, params will be linked to the route parameters and query to the GET parameters. But how can I log the post parameters? The req.param() method requires an argument and doesn't returns an array if I don't provide one. Since all POST parameters must be used through req.param(), there is no way to log them for now. Or am I missing something?
My goal is to efficiently log all parameters to debug easily the application during development, let me know if you have a better way.
You're looking for req.body, which contains the parsed POST body. (assuming you have middleware that parses it)

Categories

Resources