Question

I am using JSON and YQL to access some data from an external domain.

http://jsfiddle.net/NdkPU/22/

But the problem I am having is that the code only works intermittently, sometimes I get a response, sometimes nothing is returned.

Obviously this is a very slow solution to access this data - so one reason I thought might be that it is timing out or there is a problem with the callback function.

Any ideas why the result is not working all the time?

// Start function when DOM has completely loaded 
    $(document).ready(function(){ 

  requestCrossDomain("http://api.fool.com/caps/ws/Ticker/GOOG/Pitches/10?apikey=rW3550aXdsuJPg4bKEemC13x39jDNR6f", function (result) {
    var num = 1;

    var browserName = navigator.appName;
    var doc;
    if (browserName == 'Microsoft Internet Explorer') {
        doc = new ActiveXObject('Microsoft.XMLDOM');
        doc.async = 'false'
        doc.loadXML(result.results);
    } else {
        doc = (new DOMParser()).parseFromString(result.results, 'text/xml');
    }
    var xml = doc;

    console.log("Data Loaded: " + xml);
// Build an HTML string
myHTMLOutput = '';
 myHTMLOutput += '<table width="98%" border="1" cellpadding="0" cellspacing="0">';
  myHTMLOutput += '<th>Text</th>';

// Run the function for each student tag in the XML file
$('Pitch',xml).each(function(i) {
  PitchText = $(this).find("Text").text(); 

  // Build row HTML data and store in string
  mydata = BuildStudentHTML(PitchText);
  myHTMLOutput = myHTMLOutput + mydata;
});
myHTMLOutput += '</table>';

myHTMLOutput += '<br>Rating: ';

myHTMLOutput += $('Ticker',xml).attr("Percentile");

// Update the DIV called Content Area with the HTML string
$("#ContentArea").append(myHTMLOutput);
  });
});



 function BuildStudentHTML(PitchText){

  // Build HTML string and return
  output = '';
  output += '<tr>';
  output += '<td>'+ PitchText + '</td>';
  output += '</tr>';
  return output;
}


// Accepts a url and a callback function to run.
function requestCrossDomain(site, callback) {

// If no url was passed, exit.
if (!site) {
    alert('No site was passed.');
    return false;
}

// Take the provided url, and add it to a YQL query. Make sure you encode it!
var yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from xml where url="' + site + '"') + '&format=xml&callback=?';

// Request that YSQL string, and run a callback function.
// Pass a defined function to prevent cache-busting.
$.getJSON(yql, cbFunc);

 function cbFunc(data) {
    // If we have something to work with...
    if (data.results[0]) {
        if (typeof callback === 'function') {
            callback(data);
        }
    }
    // Else, Maybe we requested a site that doesn't exist, and nothing returned.
    else throw new Error('Nothing returned from getJSON.');
}
}
Était-ce utile?

La solution

It looks like it's just a slow API. I wrote a simple bash loop that timed that query using curl, and here were the timings:

 7.4 sec
11.1 sec
11.2 sec
 7.4 sec
11.1 sec
 7.3 sec
10.2 sec
11.8 sec
11.3 sec
 7.1 sec
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top