Pregunta

Estoy teniendo la creación de un archivo html simple usando javascript para mostrar los resultados de la consulta YQL dificultad.

entiendo cómo configurar la instrucción de selección (ejemplo: seleccione título, resumen, URL desde donde search.web consulta = "pizza") en la consola de YQL. Pero no sé cómo mostrarlo en el archivo html?

¿Alguien puede ayudar a la hora de explicar cómo mostrar los resultados de esa declaración? Los fragmentos de código sería apreciada!

Por cierto, he leído los Documentos YQL pero son un tanto complicada.

¿Fue útil?

Solución

La única manera de recuperar los resultados YQL a través de JavaScript del lado cliente es JSON-P (o usando un proxy adicional). Aquí hay un contenedor para el servicio YQL:

function YQLQuery(query, callback) {
    this.query = query;
    this.callback = callback || function(){};
    this.fetch = function() {

        if (!this.query || !this.callback) {
            throw new Error('YQLQuery.fetch(): Parameters may be undefined');
        }

        var scriptEl = document.createElement('script'),
            uid = 'yql' + +new Date(),
            encodedQuery = encodeURIComponent(this.query.toLowerCase()),
            instance = this;

        YQLQuery[uid] = function(json) {
            instance.callback(json);
            delete YQLQuery[uid];
            document.body.removeChild(scriptEl);
        };

        scriptEl.src = 'http://query.yahooapis.com/v1/public/yql?q='
                     + encodedQuery + '&format=json&callback=YQLQuery.' + uid; 
        document.body.appendChild(scriptEl);

    };
}

Uso:

// Construct your query:
var query = "select * from rss where url='somefeed.com' limit 1";

// Define your callback:
var callback = function(data) {
    var post = data.query.results.item;
    alert(post.title);
};

// Instantiate with the query:
var firstFeedItem = new YQLQuery(query, callback);

// If you're ready then go:
firstFeedItem.fetch(); // Go!!

Más información http: //james.padolsey.com/javascript/using-yql-with-jsonp/

Otros consejos

Este es un pequeño ejemplo para usted. Lo hice utilizar el sitio YQL:

<html>  
  <head>       
  </head>  
  <body>   
    <script>  
      function top_stories(o){ 
        // parse through the output here:
        var items = o.query.results.item;
        // do whatever you want with the output here:  
        console.log(items[0].title);
      }  
    </script> 
    <script src='http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20rss%20where%20url%3D%22http%3A%2F%2Frss.news.yahoo.com%2Frss%2Ftopstories%22&format=json&diagnostics=false&callback=top_stories'></script>  
  </body>  
</html>

Todo lo que hace que obtener la primera noticia de portada Yahoo! 'S

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top