You cannot make cross domain AJAX calls using JSON. You need to use JSONP. So instead of returning a regular JsonResult
from your controller action write a custom action result that will wrap the JSON in a callback that is passed as parameter:
public class JsonpResult : ActionResult
{
private readonly object _obj;
public JsonpResult(object obj)
{
_obj = obj;
}
public override void ExecuteResult(ControllerContext context)
{
var serializer = new JavaScriptSerializer();
var callbackname = context.HttpContext.Request["callback"];
var jsonp = string.Format("{0}({1})", callbackname, serializer.Serialize(_obj));
var response = context.HttpContext.Response;
response.ContentType = "application/json";
response.Write(jsonp);
}
}
and then have your controller action return this custom action result:
public ActionResult SomeAction()
{
var result = new[]
{
new { Id = 1, Name = "item 1" },
new { Id = 2, Name = "item 2" },
new { Id = 3, Name = "item 3" },
};
return new JsonpResult(balances);
}
Now you could consume this action cross domain:
var url = "http://example.com/SomeController/SomeAction/";
$.getJSON(url + '?callback=?', function (data) {
alert(data);
});