Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

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?

share|improve this answer
add comment

@ Niranjan I tried the solution but I am getting only an array of Objects

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

Output --------- [object Object],[object Object],[object Object]

share|improve this answer
    
Please provide a solution for this –  user.net Dec 9 '11 at 8:59
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.