mans
mans

Reputation: 18148

Ajax getting JSOn data doesn't work

I am trying to call an MVC method via Ajax. The code on client is as follow:

 var serviceUrl = "/Regions/GetRegionPoints";

$.get(serviceUrl, { id:region_id}, function (data) { alert("Data Loaded: " + data); });

and my MVC method is:

   public JsonResult GetRegionPoints(string id)
    {
        var model = GetRegionPoints();
        if (model.Any())
        {
            return new JsonResult(){Data = model};
        }
        return new JsonResult();
    }

I can see that the client calls Ajax and then my action is called, but there is no success and there is no data on client ( alert("data loaded") ) is not called.

What is the problem?

Upvotes: 0

Views: 145

Answers (1)

Mikolaj Kieres
Mikolaj Kieres

Reputation: 4405

It's probably because you're using GET method and if so you've to return result like this (if of course the javascript is correct and the method is fired)

return new JsonResult()
{                
     // Some Data
     JsonRequestBehavior = JsonRequestBehavior.AllowGet
};

Upvotes: 1

Related Questions