0

i am trying to get jsonp call working with jquery and my asp.net-mvc site. I found this article which i tried to copy but i am not getting a breakpoint in my callback:

Here is my jquery code:

         $.ajax({
                url: "http://www.mySite.com/MyController/Terms",
                type: "GET",
                dataType: "jsonp",
                timeout: 10000,
                jsonpCallback: "localJsonpCallback"
            });

            function localJsonpCallback(json) {
                 var terms = json.HTMLText;
                 $("#term2").html(terms);
            }

and here is my controller code:

    public JsonpResult Terms()
    {
        var data = GetData();
        return this.Jsonp(data);
    }

where JsonpResult and this.Jsonp are defined as per this page:

so I can't seem to get a callback but when I open up firebug script section, I do see a file listed when is the url about and has:

 Terms?callback=localJsonpCallback

and when i look into the content I see the correct json object content:

localJsonpCallback({"HTMLText":"PRIVACY POLICY: Your privacy is very important to us"});

so this tells me the data is coming back to the client but the callback doesn't seem to be firing and the text input is not being populated.

Can anyone find an issue with what i am doing or have any explanation on why this wouldn'nt work?

leora
  • 188,729
  • 360
  • 878
  • 1,366

1 Answers1

1

You don't have a success function defined for your ajax request. The jsonpCallback parameter that you are using only defines the name of the query string parameter that will be used by the server to wrap the JSON response into.

So try like this:

$.ajax({
    url: 'http://www.mySite.com/MyController/Terms',
    type: 'GET',
    dataType: 'jsonp',
    timeout: 10000,
    jsonp: 'jsoncallback',
    success: function(json) {
        var terms = json.HTMLText;
        $('#term2').html(terms);
    }
});

Also checkout the following answer.

Community
  • 1
  • 1
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • I still don't get the callback using the format you listed in your answer . . any other thoughts? In fact, with this way, I now no longer even see this request in firebug at all. – leora Sep 26 '11 at 12:45
  • I actually got it working using your answer from another question: http://stackoverflow.com/questions/4795201/asp-net-mvc-3-jsonp-does-this-work-with-jsonvalueproviderfactory – leora Sep 27 '11 at 02:52