Question

I'm trying to get raw content of my gist so that I could display.

Here is the code:

function requestCrossDomain(url, cb) {

    yql = "http://query.yahooapis.com/v1/public/yql?" +
          "q=" + encodeURIComponent('select * from html where url="' + url + '" ') +
          "&callback=?";

    $.getJSON(yql, function (data) {
        if(data.results[0]){
            console.log(data.results[0]);
        }
    });
}

requestCrossDomain("https://gist.githubusercontent.com/gongzhitaao/11357604/raw/7946d46c975337084b18ff1d59530acc59c9e010/index.html");

The data.results[0] does contain something, but it's garbage. The code between <script></script> are separated by a <p> (in the render function) ??? Why? Where am I wrong?

Here is a jsfiddle, as you can see, only part of the code between <script> is shown here.

Was it helpful?

Solution

Finally I solved the problem. It appears to be the parameter compat="html5". The default html spec is html4. And also I need the xpath parameter to select the <header> content as well as the <body> content. Hope the following code could be of some help to others:

function requestCrossDomain(url, cb) {

    yql = "http://query.yahooapis.com/v1/public/yql?" +
        "q=" +
        encodeURIComponent('select * from html where url="' + url + '" ') +
        encodeURIComponent('and compat="html5" and xpath="//html/head|//html/body"') +
        "&format=xml&callback=?";

    $.getJSON(yql, function (data) {
        if(data.results)
            cb(data.results);
    });
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top