JSONP json callback method not called : parsererror

醉酒当歌 提交于 2019-12-24 15:10:28

问题


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?


回答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



来源:https://stackoverflow.com/questions/19435064/jsonp-json-callback-method-not-called-parsererror

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!