.ajax JSONP parsererror

14,028

contentType: 'application/json; charset=utf-8' Tells your server that you are sending JSON data, but you don't have any data you are sending. Try leaving that setting out.

If you were to brows to your url in the browser do you get json back?

I'm not sure if this would matter, but I would remove the error setting because it says in the jQuery Ajax documentation that This handler is not called for cross-domain script and cross-domain JSONP requests.

I would try to run this with the least amount of configuration like this:

$.ajax({
    url:'http://IP/Service/api/DivisionSearch/GetAllDivisions',
    dataType: 'jsonp',
    success: function(data) { console.log(data); }
});

See if this works and then build on top of it. Without jsfiddle it's hard to debug what's going on.

Here is a link that should be a good resource for you: Basic example of using .ajax() with JSONP?

Share:
14,028
user1202606
Author by

user1202606

Updated on October 23, 2022

Comments

  • user1202606
    user1202606 over 1 year

    I'm trying to use an ajax call to bring back data from a web api. I wrote 2 similar functions and neither work. I can see the data come back through Fiddler, but it won't go to the success call, for both of the functions below. What am I doing wrong? The data comes back in both functions in Fiddler, it just doesn't go to success.

    Here is attempt 1:

        function PopulateDivisions()
    {
        $.support.cors=true;
    
    
        $.ajax({
            type:'GET',
            url:'http://IP/Service/api/DivisionSearch/GetAllDivisions',
            data: {},
            contentType: 'application/json; charset=utf-8',
            dataType: 'jsonp',
    
            success: function(data) {
    
                    alert(data);
    
                    $("#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);
            }
        });
    
    }
    

    Error: jQuery19102671239298189216_1382022403977 was not called : parser error

    Here is the data Fiddler is showing comes back:

        [{"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 attempt #2:

        function PopulateDivisions2()
    {
    
    
        $.support.cors=true;
    
    
        $.ajax({
            type:'GET',
            url:'http://IP/Service/api/DivisionSearch/GetAllDivisionsJsonP',
            data: {},
            contentType: 'application/json; charset=utf-8',
            dataType: 'jsonp',
            jsonp: false,
            jsonpCallback: "myJsonMethod",
    
            success: function(data) {
                    //data = JSON.parse(data):
                    alert(data);
    
                    $("#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("PopulateDivisions2:  " + thrownError + " : " + ErrorText);
            }
    
        });
    
    }
    

    Error: myJsonMethod was not called : parsererror

    Here is the data Fiddler shows is coming back:

        "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 \"}]);"