How to pass data using jquery ajax

I am using ASP.NET Webforms

and I have the following function that returns multiple values lat

and lon

:

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 it won't give me the output like below:

enter image description here

Now I want to send this data to a C # method. For this I 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 doesn't work.

From the method, javascript ajax

it will not pass any values ​​to the C # web method, and I cannot insert all the values ​​into my database.

So how can I accomplish this task?

+3


source to share


2 answers


Try this: -

 data: JSON.stringify({ 'lat': point.lat() })

      



Use a function JSON.Stringfy

when passing your json data.

+3


source


I noticed an error in your web method

public static string gpsdata(string lat) 

      



it 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;
    }
});

      

+1


source







All Articles