【问题标题】:Json request with YQL and MooTools使用 YQL 和 MooTools 的 Json 请求
【发布时间】:2012-07-03 10:07:30
【问题描述】:

我正在处理另一个域上的一些 json 文件,因此我尝试使用 YQL 作为代理来发出跨域请求。我是 javascript 和 web 技术的初学者,我编写的每一行代码都可能会更好,但现在如果我在你的帮助下编写的代码不是那么优雅,这不是问题。

现在我的代码是:

function GetUrl() {
    var link = "http://m.airpim.com/json/public/search?q=variabile&k=&e=1",
        name = document.id('s').get('value') || '*';

    return link.replace("variabile", name);
}

function Ricerca() {
    var yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from json where url="' + GetUrl() + '"') + '&format=json&diagnostics=false&callback=';
    return yql;
}

function LavoroJson() {
    var ciao = new Request.JSONP({
        url: Ricerca(),
        onComplete: function(data) {
            // Log the result to console for inspection
            alert(ciao.toSource());
        }
    }).send();
}

在我的想法中,我应该使用 YQL 来完成 json 的请求,但它不起作用。我该怎么做?

【问题讨论】:

    标签: javascript json mootools yql


    【解决方案1】:

    您可以稍微扩展 Request.JSONP 类。

    Request.YQLJSON = new Class({
        // gets basic info such as country and latitude data
        Extends: Request.JSONP,
    
        options: {
            log: !true,
            url: "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20json%20where%20url%3D%22{location}%22&format=json"
        },
    
        initialize: function(location, options) {
            this.parent(options);
            if (!location)
                return;
    
            this.options.url = this.options.url.substitute({location: encodeURIComponent(location)});
        },
    
        success: function(data, script) {
            this.parent(data, script);
        }
    });
    

    您可以为 airpim 细节制作自己的类似 DSL 的实现:

    Request.airpim = new Class({
    
        Extends: Request.YQLJSON,
    
        options: {
            suburl: "http://m.airpim.com/json/public/search?q={search}&k=&e=1"
        },
    
        initialize: function(query, options) {
            this.parent(this.options.suburl.substitute({
                search: encodeURIComponent(query)
            }), options);
        }
    
    });
    

    这样使用:

    new Request.airpim("*", {
        onSuccess: function(data) {
            console.log(data.query.results.json);
        }
    }).send();
    

    https://tinker.io/c9634

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多