Reputation: 10812
I am making a request to an app that I'm running on another server. I know my browser supports geolocation, because I've tried it elsewhere. Here is what I currently have:
<script>
function getLocation(){
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(showPosition)
}else{
console.log("Geolocation is not supported");
}
}
function showPosition(position){
return position.coords.latitude+" "+position.coords.longitude;
}
(function () {
var latlong = getLocation();
console.log("Latlong " + latlong);
var http = new XMLHttpRequest();
http.open("POST", "http://api.example.com/", true);
http.setRequestHeader("Content-type","application/x-www-form-urlencoded");
var params = "location=" + latlong;
http.send(params);
http.onload = function() {
alert(http.responseText);
}
})();
</script>
The request works, but the location param is set to undefined
. Why is it not defined?
Upvotes: 0
Views: 813
Reputation: 142
Move the Ajax request into the showPosition
callback. At the moment you call params before it is filled.
Upvotes: 0