0

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for your response. Then how can i get json data from that url ? do i need to use simple ajax request ? and can you please tell me about jsonp response ? i.e. for which type of data we use jsonp if not for json?
Since that URL doesn't have any CORS headers, there is no way for a webpage hosted on a different site to instruct a browser to fetch data from it. You'd need to copy the data to another URL (e.g. by running a proxy on your own server).
A description of the JSONP format can be found on Wikipedia

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.