2

I have the following ajax function using jsonp:

    function PopulateDivisions1()
{
    $.support.cors=true;


    $.ajax({
        type:'GET',
        url:'http://IP/Service/api/DivisionSearch/GetAllDivisions?callback=?',
        dataType: "jsonp",
        //jsonp: false,
        jsonpCallback: "myJsonMethod",

        success: function(data) {

                alert('yes');

                $("#divisionSelect").append($('<option></option>').val("-99").html("Select One"));
            $.each(data, function(i, item){
                $("#divisionSelect").append($('<option></option>').val(item.Name).html(item.Name));
            });
        },
        error: function(xhrequest, ErrorText, thrownError) {
            alert("Original: " + thrownError + " : " + ErrorText);
        }
    });


}

I am getting the following error: myJsonMethod was not called : parsererror

If I look at Fiddler, I am getting the following data back, I added the callback name to the front, as I saw that suggested, if I take it out it still doesn't work.

    "myJsonMethod([{\"Id\":1,\"Description\":\"Executive\",\"Name\":\"Executive \"},{\"Id\":2,\"Description\":\"ASD\",\"Name\":\"Administrative Services Division \"},{\"Id\":3,\"Description\":\"COM\",\"Name\":\"Communications \"},{\"Id\":4,\"Description\":\"CP\",\"Name\":\"Contracts and Procurement \"},{\"Id\":5,\"Description\":\"PMD\",\"Name\":\"Program Management Division \"},{\"Id\":6,\"Description\":\"RED\",\"Name\":\"Research and Evaluation Division \"},{\"Id\":7,\"Description\":\"IT\",\"Name\":\"Information Technology \"}])"

Here is the method in my controller:

     public string GetAllDivisions(string callback)
    {
        var divisions = _DivisionModel.GetAllDivisions();

        var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();

        string json = serializer.Serialize(divisions);

        string result = callback + "(" + json + ");";

        return result; 
    }

I'm not getting to my success call, what am I missing or doing wrong?

user1202606
  • 1,160
  • 9
  • 23
  • 37

1 Answers1

-1

You dont have to specify a success callback because the jsonp callback will happen automatically as the server side code will return javascript in case of jsonp.

Refer the below answer for working example.

Using jquery jsonp returns error callback function was not called

Community
  • 1
  • 1
Saranya
  • 1,988
  • 16
  • 20