2011-08-23 19 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/ –