When attempting to send both the first name and last name to an API request using jQuery's get method, I encountered an issue. The request works perfectly when only the first name is included, but encounters a problem when adding the last name. The request functions correctly with the URL "http://localhost:5000/name?firstname="+h1;, however it fails when the URL is set to "http://localhost:5000/name?firstname="+h1+"&lastname="+h2;. In this latter case, the desired output briefly appears and then disappears, while the URL changes to "http://localhost:5000/?", with the function being called from "http://localhost:5000/" Here is the JavaScript code:
<script type="text/javascript>
$(document).ready(function(){
$("#submitButton").click(function(e){
var h1 = $("#handle1").val();
var h2 = $("#handle2").val();
var u = "http://localhost:5000/name?firstname="+h1+"&lastname="+h2;
alert(u);
$.get(u, function(data){
$('.result').html(data);
})
});
});
</script>
And here is my Express API code:
var express = require('express');
var app = express();
var path = require("path");
const PORT = process.env.PORT || 5000
app.listen(PORT, function(){
console.log('server running on port ' + PORT);
})
app.get('/name', function(req, res){
res.send("Full Name: "+ req.query.firstname + " " + req.query.lastname);
});