1

Possible Duplicate:
Ajax cross domain call

I have Asp.Net Controller methods which return JSON response. However I am unable to call it from a different domain using Jquery's $.getJSON(...){}

Quick googling suggests that JSONP must be used. Question is what is the difference between JSON and JSONP? Is that just another new term? And if JSONP is working why I can't I make JSON work? Is there any way to not use these fancy JSONP and use plain old JSON because I am afraid of any changes to my server side code.

Community
  • 1
  • 1
Jack
  • 7,433
  • 22
  • 63
  • 107
  • does this help http://en.wikipedia.org/wiki/JSONP – OJay Sep 25 '12 at 03:59
  • A regular JSON ajax will not work because it violates the same origin policy, which is usually held throughout all modern browsers. This policy does not allow requests with javascript to another domain, as many security concerns arise, specifically browser stored cookies and sessions. Read more from wikipedia if you are interested. – Wiz Sep 25 '12 at 04:02
  • You might want to look into cross-origin resource sharing (CORS). – JayC Sep 25 '12 at 05:00

3 Answers3

5

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);
});
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
0
JSONP = JSON + PADDING 
      = additional JavaScript callback function wrapping the JSON content.

In JavaScript all ajax requests have this cross domain policy criteria to follow. which prevents the XMLHttpRequest object requesting resources on a different domain.

As JSON is valid JavaScript, we can not use it. JSONP was created as a method of retrieving data from some remote domain by writing a callback function and dynamically adding a script tag to the DOM, the script tag will call the callback on load and the data is then available to the application.

To make a request to a server JSONP enabled, we need to send some information about our site so that the server can create a response which we can use.

A Developer
  • 1,001
  • 3
  • 12
  • 32
0

You cannot make jsonp work on all cross domain sites except the one which allows you by telling you to provide some information about your server using extra call back (value or name).

If other site don't allow your provided call back you get nothing from that.

Zahid Riaz
  • 2,879
  • 3
  • 27
  • 38