leora
leora

Reputation: 196459

Jsonp result not working on my asp.net-mvc site

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?

Upvotes: 0

Views: 921

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038730

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.

Upvotes: 1

Related Questions