HELP!
First timer using AXIOS, and what I’m trying to do is make a server-side call, and assign the server response to a JS variable as an array. So that I can pass that array to a function.
I’m running out of ideas on how to make that happen… I can get a response as a “string” from the server, but don’t know if it’s possible to get an array object response from server… I think not. So next option is make the server give out a JSON response, then convert that to an array of objects. But can’t quite figure it out.
In the end, I ended up using eval( ) and that doesn’t seem to be a good idea?
So on my homepage, I have this script.
It works, and produces a map with the correct map markers in the right locations.
<script>
var map = new GMaps({
div: '#map',
lat: 37.02009820136811, // center of USA map
lng: -97.84423828125,
zoom: 5
});
// NOTE: drop multiple map markers, use an array of marker objects
axios.get('/api-jobs/LatestJobsMapMarkers.cshtml')
.then(function (response) {
eval(response.data); // <---- BAD IDEA?
map.addMarkers(moreJobs);
})
.catch(function (error) {
console.log(error);
});
</script>
I make a GET call to ‘LatestJobsMapMarkers.cshtml’ and the server returns this string as it’s response… which is then eval( ), and then the map drawn.
var moreJobs = [
{
lat: 41.90227704096373,
lng: -87.9345703125,
title: 'Chicago, IL',
infoWindow: {
content: '<b>Big Data R Programmer</b><br>Chicago, IL'
}
},
{
lat: 33.10074540514424,
lng: -116.71875,
title: 'San Diego, CA',
infoWindow: {
content: '<b>Systems Programmer</b><br>San Diego, CA'
}
},
{
lat: 36.13787471840729,
lng: -86.8359375,
title: 'Nashville, TN',
infoWindow: {
content: '<b>Web Developer</b><br>Nashville, TN'
}
},
{
lat: 29.42165652325403,
lng: -98.48419189453125,
title: 'San Antonio, TX',
infoWindow: {
content: '<b>Data Analyst</b><br>San Antonio, TX'
}
},
{
lat: 39.740986355883564,
lng: -104.96337890625,
title: 'Denver, CO',
infoWindow: {
content: '<b>Nodejs Developer</b><br>Denver, CO'
}
},
{
lat: 36.7498564123657,
lng: -106.12597456325,
title: 'MISSING, UK',
infoWindow: {
content: '<b>Sample Developer</b><br>Missing, UK'
}
},
];
How can I accomplish the same thing without using eval( ) ?