0

I'm a beginner in coding, and I am making a front and back end prototype website. Written with node/express/javascript

I have a Google Map API, where when I click a button I can console log the location. I am able to retrieve the co-ordinates via the console, but I want to take these coordinates and store them in my mongodb to then post back onto my map api. Any ideas of the best way to do this? Preferably using ajax?

CCY
  • 1
  • An AJAX call made from Javascript will be able to pass that info to your server, where you can store it in the DB. – Reinstate Monica Cellio Mar 15 '17 at 12:54
  • so would be something like... $.ajax('http://localhost:3000/homepage', { data: { coordinates: {latitude, longitude} } }) .then( function success(coordinates) { alert('User\'s coordinates are' + coordinates); }, function fail(data, status) { alert('Request failed', status); } ); – CCY Mar 15 '17 at 12:58
  • which is your backend? try posting your code. – Abhinav Mar 15 '17 at 12:59
  • That looks about right, yes. Just give it a go and see how you get on. Obviously you'll need something on the server to handle the posted data. – Reinstate Monica Cellio Mar 15 '17 at 12:59
  • im struggling to link it to the back end... trying to start with the pull from the browser first to post to mongo... im a beginner in this so not really sure what im doing be it right or wrong – CCY Mar 15 '17 at 13:15
  • Have a look at this question. It should hopefully provide something for you to start with... http://stackoverflow.com/questions/12006417/node-js-server-that-accepts-post-requests – Reinstate Monica Cellio Mar 15 '17 at 14:05

1 Answers1

0

typically you need to have ajax call to your server from javascipt/jquery

<script>
$(function(){
$.ajax({
type: "POST",
URL: [server url],
data: [data to be sent to server]
success: function (response){
//this is your server callback 
}
})
});
</script>
Abhinav
  • 1,202
  • 1
  • 8
  • 12