I'm doing a simple project to practice node js along with Jquery Ajax. So I have an ajax post request that sends some data to nodejs server and wait for a response. In the server-side, I have a code that reads the data and do something with it. This works fine. But when I try to send the response back to ajax, the page changes and it shows the response in plain text. What I want is to do something with the response in ajax side. Sorry if I'm not clear, but hope you understand when reading the code.
Jquery Ajax code:
$("submitBtn").on("click", e=>{
e.preventDefault();
$.ajax({
url: "/getCity",
type: "POST",
data: `city=${cityName}&country=${countryName}`,
success: function(data){
console.log(data);
}
});
});
server-side code:
app.post("/getCity", (req, res)=>{
//a promise
.then(cityID=>{
res.status(200).send(cityID.toString());
});
});
html code:
<form method="post" action="/getCity">
<input/>
<input/>
<button id="submitBtn" type="submit">Search Weather</button>
</form>
I included html code because I don't know if putting method="post" action="/getCity"
is necessary even though making the ajax post request.
I hope you help me with this and if possible the html thing.
Thank you in advance.