0

I trying to call cross domain call from jquery. The functions steps into error section with the error message, "alertResponse was not called". I am using Jquery version "jquery-1.6.2" and I am able to view the result of action checkSession in browser using "http://localhost:123/Home/CheckSession". Also in the visual studio debugger, the browser is loaded with the actual result returned from above url. But I am unable to get the value from the jquery.

Please find the Jquery code below: The Jquery code is in different domain - http://localhost:7451/TestPage.aspx

       $(document).ready(function () {
           SetActionItemCount();
         });
         function SetActionItemCount() {
               var URL = "http://localhost:123/Home/CheckSession";
                function alertResponse(data, status) {
                     alert("data: " + data + ", status: " + status);
                     }
                 $.ajax({
                 url: URL,
                  dataType: 'jsonp',
                  jsonp: 'callback',
                  jsonpCallback: 'alertResponse',
                  success: function (jsonp) {
                            alert(jsonp);
                            },
                  error: function(xhr, status, error) {
                                alert("error");
                            }
                  });
              }

Server side MVC action method - this code is in "http://localhost:123/Home/CheckSession"

    public JsonResult CheckSession()
    {
        string status = string.Empty;
        status = "test data";               
        return Json(new { status },JsonRequestBehavior.AllowGet);
    }

I am not sure what is that I am missing here. Also, when I run the jquery with debugger attached to localhost:32088, I get the debugger in checksession action method and it does return the value. But I am unable to get the value in jquery.

The output that I receive when I try the url "http://localhost:123/Home/CheckSession" in browser is {"status":"test data"}

Any help is much appreciated.

user1081802
  • 47
  • 1
  • 1
  • 6

2 Answers2

0

I think there's some confusion here:

  • jsonpCallback is not supposed to supply a named function to act as the success callback. It's only specifying the internal name of the callback function jQuery will use to handle the JSON response - the actual code for that function is specified by the success parameter. As far as I know, the main reason to use this is to support better client-side caching - something you probably don't want in a CheckSession script. So I would drop jsonCallback altogether and put your alertResponse code in the success function.

  • Your server-side script needs to wrap the JSON response in the code <callbackName>(...);, where <callbackName> is the name of the function supplied in the request URL, and ... is your JSON. So if the request is /Home/CheckSession?callback=alertResponse, your response should look like alertResponse(<JSON>);. It looks like you're just sending back the JSON with no wrapper code.

  • jsonp: 'callback' is unnecessary (it's the default).

nrabinowitz
  • 55,314
  • 10
  • 149
  • 165
  • I am not sure on how to wrap the response in the server code.Can you please modify my piece of code, that would be great help. – user1081802 Dec 14 '11 at 22:46
  • Sadly, I don't know asp.net, so I can't rewrite that code. It's just string concatenation - you'd need to get the value of the "callback" parameter, then return `callbackName + "(" + json + ");"`. – nrabinowitz Dec 14 '11 at 23:10
0

You need to return JSONP from your server, not JSON. Take a look at the following blog post which illustrates how you can write a custom JsonpResult action result to use in your controller action. And here's a similar post.

Community
  • 1
  • 1
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928