2014-02-24 19 views
1

我正在嘗試爲Pebble寫一個簡單的表格,並且正在迎接這個javascript錯誤。通過JSON API訪問匯率通過卵石

我從http://rate-exchange.appspot.com/currency?from=usd&to=jpy

拉動信息的代碼如下所示:

function HTTPGET(url) { 
    var req = new XMLHttpRequest(); 
    req.open("GET", url, false); 
    req.send(null); 
    return req.responseText; 
} 
var getWeather = function() { 
    var lhs1 = "usd"; 
    var rhs1 = "jpy"; 
    var url1 = "rate-exchange.appspot.com/currency?from=" + lhs1 + "&to=" + rhs1 
    console.log(url1); 
    var response1 = HTTPGET(url1); 
    var json1 = JSON.parse(response1); 

等,它的推移,但你的想法。

我得到這個

[PHONE] pebble-app.js:?: JS: where.is.spot: rate-exchange.appspot.com/currency?from=usd&to=jpy 
[PHONE] pebble-app.js:?: Error: where.is.spot: Invalid URL at line 4 in pebble-js-app.js 

這是在這裏第4行失敗: req.send(NULL);

任何想法是什麼導致此錯誤? Feed似乎沒有任何問題。

回答

3

您的網址應以http://開始:

變化:

var url1 = "rate-exchange.appspot.com/currency?from=" + lhs1 + "&to=" + rhs1 

到:

var url1 = "http://rate-exchange.appspot.com/currency?from=" + lhs1 + "&to=" + rhs1 
+0

這是,很簡單,正確的一個nswer。該網址在谷歌瀏覽器中沒有http的情況下工作,但我想這只是它很聰明。我很尷尬,這很容易。謝謝。 – swyx

+0

我以前去過那裏。這就是我所知道的;) – sarfata

0

而不是

req.send(null);

使用

req.send();

0

如果您想嘗試使用jQuery:

var getCurrencyPair = function() { 

    var url = 'http://rate-exchange.appspot.com/currency?from=usd&to=jpy' 

    $.getJSON(url, function(data) { 
    console.log(data); 
    }); 

};