Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The solution jQuery came up with, is to provide an anonymous callback function like this:</p> <pre><code>jQuery.getJSON("http://mycrossdomain.com/?callback=?", function(data) { // Now I have the data }); </code></pre> <p>I think this could be adapted to your case as well.</p> <pre><code>var data = new DataRetriever(); data.GetData(function(data) { // Now I have the data }); </code></pre> <p>You could do the same thing behind the scenes in the GetData function if you didn't want to provide an anonymous function.</p> <pre><code>function GetData(callback) { // optional // Create random function name however you want var funcName = "data_" + (+new Date() + Math.floor(Math.random()*100)), // Moved this up to be available in the random function new_tag = document.createElement('script'); // This part will allow you to do the callback behind the scenes if callback isn't provided as a param callback = callback || function(data) { this.dataReturned = data; // or something } // Assign it to the window object window[funcName] = function(data) { callback(data); // Unassign this function delete window[funcName]; // Recycle the script tag document.body.removeChild(new_tag); } new_tag.type = 'text/javascript'; new_tag.src = 'http://somesite.com/somemethod?callback='+funcName; // Add the element document.body.appendChild(new_tag); } </code></pre> <p>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!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload