1

I am getting a JSON object in my javascript function in aspx page. I need to fetch this data from my code behind file. How can I do it?

My javascript function is :

 function codeAddress() 
    {
        var address = document.getElementById("address").value;
        var geocoder = new google.maps.Geocoder();            
        geocoder.geocode({ 'address': address }, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                map.setCenter(results[0].geometry.location);
                var marker = new google.maps.Marker({ map: map, position: results[0].geometry.location });
            }
            else {
                alert("Geocode was not successful for the following reason: " + status);
            }

            var requestParameter = '{' +
                        'output:"' + results + '"}';

            $.ajax({
                type: "POST",
                url: "Default.aspx/GetData",
                data: requestParameter,
                //contentType: "plain/text",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(msg) {
                    alert(msg.d);

                }, 
                error: function() { alert("error"); } 
            });

        });
    }


   Default.aspx.cs

 [WebMethod]
public static string  GetData( Object   output) 
{
    return output.ToString();
}

I am getting the output as an array of objects instead of the actual result - [object Object],[object Object],[object Object] .Please provide me a solution to get the actual result. The JSON I am working on is given below http://maps.googleapis.com/maps/api/geocode/json?address=M%20G%20Road&sensor=false

user.net
  • 51
  • 3
  • 13
  • possible duplicate of [How to post an array of complex objects with JSON, jQuery to ASP.NET MVC Controller?](http://stackoverflow.com/questions/320291/how-to-post-an-array-of-complex-objects-with-json-jquery-to-asp-net-mvc-control) – Jan Hančič Dec 08 '11 at 11:39

1 Answers1

3

Create a WebMethod on your aspx Page that get the array on the page as:

[WebMethod]
public static GetData( type[] arr)  // type could be "string myarr" or int type arrary - check this
{}

and make json request.

$.ajax({
  type: 'POST',
  url: 'Default.aspx/GetData',
  data: "{'backerEntries':" + backerEntries.toString() + "}",
  contentType: 'application/json; charset=utf-8',
  dataType: 'json',
  success: function(response) {}
});

or you can use .post().

Check the url: in ajax request GetData WebMethod must be declared on the Default.aspx on that page where you are making ajax request.

Check this question to know that how to format array for sending to the web method.
Why doesn't jquery turn my array into a json string before sending to asp.net web method?

Check these links for reference:
Handling JSON Arrays returned from ASP.NET Web Services with jQuery - It is Best to take idea
How to post an array of complex objects with JSON, jQuery to ASP.NET MVC Controller?

Community
  • 1
  • 1
Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75