0

Hi I am trying to populate markers on google map and using Json to read latitude and longitude from database.

As a first step all I am trying to do is to see if I am getting any response back. The code is below:

Entities is as follows:

public class Property
{
    public int PropertyId { get; set; }
    ...
    public virtual PropertyAddress PropertyAddress { get; set; }
}

public class PropertyAddress
{
    [Key]
    public int PropertyId { get; set; }
    ...
    public float Latitude { get; set; }
    public float Longitude { get; set; }
}

Json Property Model:

public class JsonProperty
{
    public int PropertyId { get; set; }
    public float Latitude { get; set; }
    public float Longitude { get; set; }
}

Controller:

    public ActionResult Map()
    {
        if (Request.IsAjaxRequest())
        {
            var properties = websiteRepository.FindAllProperties();

            var jsonProperties = from property in properties
                                 select new JsonProperty
                                 {
                                     PropertyId = property.PropertyId,
                                     Latitude = property.PropertyAddress.Latitude,
                                     Longitude = property.PropertyAddress.Longitude
                                 };

            return Json(jsonProperties.ToList());
        }
        else 
        {
            return View();
        }
    }

View:

@section Scripts_footer {
    <!-- Google Map Script -->
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>

    <script type="text/javascript">
        $(document).ready(function () {
            var map = new google.maps.Map(document.getElementById("map"), {
                center: new google.maps.LatLng(19.0759837, 72.8776559),
                zoom: 13,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            });

            $.getJSON("/Search/Map", function (json) {
                alert(json);
            });
        });
    </script>
}

<div id="map" style="width: 500px; height: 300px"></div>

I was expecting an alert message with object object object ... not really sure why that is not happening.

4
  • You could have shortened your question to How to debug getJSON or try to do some research first. Commented Oct 8, 2012 at 11:41
  • I would have but I wasn´t sure what the issue is .. I am really new to web development .. Apologies Commented Oct 8, 2012 at 11:46
  • Firstly, I would check for errors in Chrome Console.. Anything? Commented Oct 8, 2012 at 11:50
  • It says "Failed to load resource: the server responded with a status of 500(internal server error)" Commented Oct 8, 2012 at 12:06

1 Answer 1

2

Change the return statement in the controller code to:

return Json(jsonProperties.ToList(), JsonRequestBehavior.AllowGet);

Json requests are implicitly blocked for security reasons.

Sign up to request clarification or add additional context in comments.

2 Comments

Perfect thank you .. The tutorial I learnt this from didn´t require it. And they were also performing a get operation.
I wonder how many have done the same mistake, it's so common.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.