Unable to send data to Node.js from my HTML/AJAX, I have variable selectValue which I want to send to my Node server. While making use of data: { selectValue: selectValue} does not help.
index.html
<script type="text/javascript">
$(document).ready(function(){
var selectElement = document.getElementById('selectDetails');
var selectValue='';
$.ajax({
url: "http://localhost:8070/api/route1",
type: 'POST',
dataType:'json',
success: function(res) {
console.log(res);
console.log(res.content);
$.each(res.content, function(key,value) {
console.log(value);
selectElement.options[selectElement.options.length] = new Option(value.hostname+":"+value.port);
});
$("#myButton").click(function(){
var selectValue = document.getElementById("selectDetails").value;
console.log(selectValue);
});
},
data: { selectValue: selectValue}
});
});
</script>
app.js
router.post('/route1', function(req, res){
var selValue= req.body.selectValue;
console.log("Select Value"+selValue);
});
console.log("Select Value"+selValue); give an undefined value. How do I send the value of selectValue to my node server.
var router = express.Router();var bodyParser = require('body-parser'); app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.json())console.log(selectValue)i do see the value in the browser but the same is not being reflected on my node server irrespective of get/post call