This is my ajax script for getting user latitude and logitude based on location it is working properly in my localhost but in my beta application unable to get the values pls help me to fix it any other suggestion pls thanks in advance.
JavaScript
function getLocation() {
debugger
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
alert("Geolocation is not supported by this browser.");
}
}
function showPosition(position) {
debugger
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
// Send the location data to the server using AJAX
$.ajax({
type: "POST",
url: "Login.aspx/SaveLocation",
data: JSON.stringify({ latitude: latitude, longitude: longitude }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
console.log("Location saved successfully:", response);
},
error: function (xhr, textStatus, errorThrown) {
console.error("Error saving location:", textStatus, errorThrown);
console.log("Response Text:", xhr.responseText);
}
});
}
getLocation();C#
public static string Latitude;
public static string Longitude;
[WebMethod]
public static void SaveLocation(string latitude, string longitude)
{
Latitude = latitude;
Longitude = longitude;
}
Jignesh KumarRank #91 · 22.9K reputationPosted Dec 25, 2023, 11:57 PM EST
Hello Jaya Prakash,
Have tried to add console log to check its getting value or not,
function getLocation() { debugger if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showPosition); } else { alert("Geolocation is not supported by this browser."); } } function showPosition(position) { debugger var latitude = position.coords.latitude; var longitude = position.coords.longitude; console.log('Latitude: ' + latitude); console.log('Longitude: ' + longitude); // Send the location data to the server using AJAX $.ajax({ type: "POST", url: "Login.aspx/SaveLocation", data: JSON.stringify({ latitude: latitude, longitude: longitude }), contentType: "application/json; charset=utf-8", dataType: "json", success: function (response) { console.log("Location saved successfully:", response); }, error: function (xhr, textStatus, errorThrown) { console.error("Error saving location:", textStatus, errorThrown); console.log("Response Text:", xhr.responseText); } }); } getLocation();Check your console log to check, you are getting values in console or not