【问题标题】:How to use YQL in JavaScript to retrieve web results如何在 JavaScript 中使用 YQL 来检索网页结果
【发布时间】:2011-11-30 05:36:56
【问题描述】:

我一直在尝试给出的代码 How to use YQL to retrieve web results?

但它不起作用。

请给我一些其他建议或纠正该代码。

我只是在 page_load 上调用一个函数

<body onload = "load_content();">

在 load_content() 方法中,我必须获取其他网站的提要并将其显示在我的 HTML 页面上。

Load_Content 方法

     var query = "select * from html where url='http://www.imdb.com/title/tt0123865/'";

    // Define your callback:
    var callback = function(data) {
    console.log("DATA : " + data);
    };

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

    // If you're ready then go:
    console.log("FEED : " + firstFeedItem.fetch()); // Go!!

函数 YQLQuery

    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);

    };
  }

数据变量中没有任何内容

【问题讨论】:

  • 请出示您的load_content()功能码。
  • 控制台是否出现任何错误?
  • 回调函数没有获取任何数据。控制台只是显示未定义

标签: javascript html json yql


【解决方案1】:

一个简单的 get 请求就是对此的回答。

 $.get("http://www.imdb.com/title/tt1243957/",
    function(data){
        console.log(data);
    }//end function(data)
 );//end getJSON

【讨论】:

    猜你喜欢
    • 2010-11-13
    • 1970-01-01
    • 2015-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-23
    • 2014-10-31
    • 1970-01-01
    相关资源
    最近更新 更多