I am using ASP.NET Webforms
and I have the following function which returns multiple lat
and lon
values:
directionsService.route(request, function (response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
if (response.routes && response.routes.length > 0) {
var routes = response.routes;
for (var j = 0; j < routes.length; j++) {
var points = routes[j].overview_path;
var ul = document.getElementById("vertex");
for (var i = 0; i < points.length; i++) {
var li = document.createElement('li');
li.innerHTML = getLiText(points[i]);
ul.appendChild(li);
}
}
}
}
});
function getLiText(point) {
var lat = point.lat(),
lng = point.lng();
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Default.aspx/gpsdata",
data: { 'lat': point.lat() },
dataType: "json",
success: function (data) {
// response(data.d);
alert(point);
},
error: function (result) {
alert("Error");
}
});
return "lat: " + lat + " lng: " + lng;
}
<div id="vertex-container">
<label>Points</label>
<ul id="vertex">
</ul>
</div>
So not it will give me output as below:
enter image description here
Now I want to send these all data to c# method. For that I have add below method:
[WebMethod]
public static string gpsdata(string lat)
{
List<string> result = new List<string>();
SqlConnection conn = new SqlConnection();
conn.ConnectionString = System.Configuration.ConfigurationManager.AppSettings["keyConn"].ToString();
using (SqlCommand cmd = new SqlCommand("Insert into latdata values(username)", conn))
{
//my code
}
}
But it is not working..
From the javascript ajax
method it will not pass the any values to the c# web method and I am not able to insert that all values to my database.
So how can I do this task?
2 Answers 2
Try this:-
data: JSON.stringify({ 'lat': point.lat() })
Use JSON.Stringfy
function while passing your json data.
1 Comment
I notice an error in your webmethod
public static string gpsdata(string lat)
should be
public static string gpsdata(string lat, string lng)
//jQuery
var geoLoc = {
lat: point.lat(),
lng: point.lat()
}
var parameter = JSON.stringify(geoLoc);
$.ajax({
type: "POST",
url: "Default.aspx/gpsdata",
data: parameter,
dataType: "json",
contentType: "application/json",
async: true,
beforeSend: function () { },
success: function (response, status, xhr) {
debugger;
},
error: function (xhr, status, error) {
debugger;
}
});