This whole jsonp thing is quite confusing…
Here is what I want to do:
- I have a class
DataRetriever - The class has a method
GetData -
GetDatamakes a jsonp request with the following code:var new_tag = document.createElement('script'); new_tag.type = 'text/javascript'; new_tag.src = 'http://somesite.com/somemethod?somedata'; // Add the element var bodyRef = document.getElementsByTagName("body").item(0); bodyRef.appendChild(new_tag);
Now, the jsonp data from the server somesite.com can call a function in my code with the data. The problem is, how does the data get delivered to the instance of DataRetriever that requested it?
I’m really stuck here.
The solution jQuery came up with, is to provide an anonymous callback function like this:
I think this could be adapted to your case as well.
You could do the same thing behind the scenes in the GetData function if you didn’t want to provide an anonymous function.
Note you will have to make sure the JSONP request accepts the callback GET parameter. If you’re using a 3rd party API they will already support this. Hope this helps!