2011-08-23 50 views
1

我想在我的网站上使用雅虎的placefinder找到人的位置,但它不工作。我一定做错了什么,但是我无法弄清楚什么。雅虎与mootools的定位器

我有以下代码:

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功能不会被调用。使用萤火虫,我可以看到请求已发送,并且收到某种回应。我收到这些响应标头:

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响应。我也在一点使用了提示器服务服务器端,没有任何问题:

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

我在做什么错?

回答

3

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

解决方法是:

的周围YQL + MooTools的+ JSONP提供的代码示例大量尽管我怀疑我的业务逻辑,用在这里 - 在不被依赖1但雅虎提供的服务非常近视,无法保证或预期高/关键性能。

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

通过从mootools的,更延伸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); 
    } 
}); 
+0

哎哟,感谢抬起头。我只尝试使用雅虎的服务,因为谷歌的地理编码器不适用于我的公司所在的海峡群岛。我想我会寻找另一种解决方案。 – Oliver

+0

这里是一个JSONP启用地理位置服务的示例代码,我倾向于使用:http://jsfiddle.net/dimitar/7KHKF/ –