雅虎吸引人与mootools

时间:2011-08-23 10:05:13

标签: javascript mootools

我正在尝试在我的网站上使用yahoo placefinder找到人们的位置,但它无法正常工作。我必须做错事,但我无法解决问题。

我有以下代码:

new Request.HTML({ method: 'get', url: 'http://where.yahooapis.com/geocode?q=1600+Pennsylvania+Avenue,+Washington,+DC&appid=KGe6P34c',
    onSuccess: function () {
        console.log("aaa");
    }
}).send();

永远不会调用onSuccess函数。使用firebug,我可以看到请求已经发送,并且收到了某种响应。我收到了这些回复标题:

Date: Tue, 23 Aug 2011 09:51:18 GMT
P3P: policyref="http://info.yahoo.com/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA POL HEA PRE LOC GOV"
X-Yahoo-Serving-Host: wws2.geotech.ch1.yahoo.com
Access-Control-Allow-Origin: *
Connection: close
Transfer-Encoding: chunked
Content-Type: text/xml; charset=utf-8
Cache-Control: private

但是回复的主体是空的。

奇怪的是,如果我在我的网络浏览器中输入request URL,我会收到正常的XML响应。我也在一点使用placefinder服务服务器端没有问题:

    String reqURL = "http://where.yahooapis.com/geocode?postal=" + HttpUtility.UrlEncode(postCode) + "&Country=" + HttpUtility.UrlEncode(countryCode) + "&appid=KGe6P34c";
    XmlDocument xml = new XmlDocument();
    xml.Load(reqURL);

我做错了什么?

1 个答案:

答案 0 :(得分:3)

您正在执行跨域XHR请求,由于安全策略,这是不允许的。

解决方法是:

为YQL + MOOTOOLS + JSONP提供代码的大量示例虽然我质疑我的业务逻辑并在这里使用 - 依赖于不是1但是2雅虎驱动的服务是非常近视的,高/关键性能不能是保证或预期。

记住正在运行的笑话,关闭任何服务的最快方法是让雅虎购买它。

通过从mootools-more:

扩展Request.JSONP的示例
Request.GeoData = new Class({

    Extends: Request.JSONP,

    options: {
        url: "http://geoip.pidgets.com/?format=json"
    },

    initialize: function(options) {
        this.parent(options);
        if (this.options.ip) {
            this.options.url += "&ip=" + this.options.ip;
        }
    }

});

new Request.GeoData({
    // default ip = client
    onComplete: function(data) {
        console.log(data);
    }
}).send();

new Request.GeoData({
    // hardwire an ip to check for:
    ip: "87.106.181.42",
    onComplete: function(data) {
        console.log(data);
    }
}).send();

您可以使用YQL进一步详细说明:

Request.getPlaceInfo = new Class({
    // return json data with extended information of a place / location.
    Extends: Request.JSONP,
    options: {
        url: "http://query.yahooapis.com/v1/public/yql?q=select * from geo.places where text='{location}'&format=json",
    },
    initialize: function(location, options) {
        this.parent(options);
        this.options.url = this.options.url.substitute({location: location});
    },
    success: function(data, script) {
        this.parent(data, script);
    }
});