Search code examples
javascriptajaxjsonp

callback function is not called in JSONP


I'm working with jsonP and new to it. I have read about it on different websites and tried a simple example but the callback function is not called. There is no error no exception in the console but still i'm unable to get the data on console. Here is the code i'm trying

function getJSONPData(){
    var url = "http://nvd3.org/examples/cumulativeLineData.json?callback=parseRequest";
    var script = document.createElement('script');
    script.setAttribute('src', url);            
    document.getElementsByTagName('head')[0].appendChild(script);
}
function parseRequest(response)
{

    try
    {
        alert("got response");
        console.log(response);
    }
    catch(an_exception) 
    {
        alert('exception occured '+an_exception);
    }
}

while in web-console of chrome, in "network" tab, I can see that the requested json file is fetched with a status of "200 OK". Also I can see the data of requested json file in "response" section of "network" tab in chrom webconsole. Why i'm not getting it directly on console when i'm trying to print it. I have debugged the code abnd found that callback method is not getting called. Why is so ? Am i doing something wrong or missing something ? Will be thankful for any help.


Solution

  • The server for the URL you are calling, http://nvd3.org/examples/cumulativeLineData.json?callback=parseRequest, is responding with a JSON document not a JSONP application.

    For the callback to fire you need a JSONP response.