I am unable to retrieve the values from Query String when using AJAX
My JavaScript
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
}
}
function showPosition(position) {
$(document).ready(function () {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
var serurl = 'http://mydummyurl.com/?lat='+latitude+'&lon='+longitude;
$.ajax({
type: 'POST',
url: serurl
});
});
}
getLocation();
And, I am trying to use
echo $_GET['lat']; // This is working fine.
echo $_GET['lon']; // This is **NOT WORKING.**
Also, if I tweak the URL like http://mydummyurl.com/?lon='+longitude+'&lat='+latitude+'
Then $_GET['lon']
is working and $_GET['lat']
is not working.
The .ajax
method expects the query string to be specified separately from the url, either as an object or string, like
$.ajax({
type: 'POST',
url: 'http://mydummyurl.com',
data: { 'lat': latitude, 'lon':longitude }
});
or
$.ajax({
type: 'POST',
url: 'http://mydummyurl.com',
data:'lat=' + latitude + '&lon=' + longitude
});
So if your server needs the values to be in $_GET
, you will need to use type: 'GET'