我想使用JQuery的JSONP功能從網站檢索氣壓數據。從JSON文件檢索數據
首先,我使用Yahoo!將XML數據轉換爲JSON的管道。然後我嘗試在警報中接收並使用這些數據,但它不起作用。在這個JSFiddle中,我有一個簡單的工作示例,但是當我嘗試使用更高級的JSON文件時,它不起作用。
另請參閱IBM提供的this article。在你的代碼
我想使用JQuery的JSONP功能從網站檢索氣壓數據。從JSON文件檢索數據
首先,我使用Yahoo!將XML數據轉換爲JSON的管道。然後我嘗試在警報中接收並使用這些數據,但它不起作用。在這個JSFiddle中,我有一個簡單的工作示例,但是當我嘗試使用更高級的JSON文件時,它不起作用。
另請參閱IBM提供的this article。在你的代碼
問題
http://
在你的鏈接你需要嘗試這個(見alert
,它會提醒標題)
<div onClick="$.getJSON('http://pipes.yahoo.com/pipes/pipe.run?_id=467a55b506ba06b9ca364b1403880b65&_render=json&textinput1=40.78158&textinput2=-73.96648&_callback=?', function(data){alert(data.value.title)})">Click Me</div><!--I can't get this to work-->
但它能夠更好地使用像下面:
<div class="loadjson">Click Me</div>
function barometer() {
$.getJSON('http://pipes.yahoo.com/pipes/pipe.run?_id=467a55b506ba06b9ca364b1403880b65&_render=json&textinput1=40.78158&textinput2=-73.96648&_callback=?', function(data) {
alert(data.value.title);
})
}
$('div.loadjson').on('click', function() {
barometer();
});
注:$.getJSON()
返回兩個parameters
data
對象之內。
- 1st one is `count` that have integer value
- 2nd one is `value`, which is an `Object`.
爲了得到第二個參數你需要使用data.value
。
對不起,我沒有螢火蟲。添加console.log有什麼意義?在你的演示中,點擊div仍然沒有提示警報。謝謝 – Ian
@Ian如果你打算開發JS開發,我認爲強烈建議學習Firebug或者如何使用其他瀏覽器的JavaScript控制檯。 http://stackoverflow.com/questions/55633/where-is-the-console-api-for-webkit-safari – KeatsKelleher
@ Ian我用'alert()'做了我的小提琴,但你應該用來學習螢火蟲 – thecodeparadox
有很多很多很多問題,我固定它和評論直列問題的代碼...
的jsfiddle這裏: http://jsfiddle.net/kritzikratzi/LQcVd/
function loadBarometerData(){
// 1. it's not generally bad practice to put stuff in the
// onClick listeners, but really... don't put such long code there! it's not readable ...
// 2. you were missing a "http://" and it was downloading
// jsfiddle.net/pipes.yahoo.com/....
// 3. getJSON doesn't return a result immediately, you need to use callbacks for that!!
$.getJSON('http://pipes.yahoo.com/pipes/pipe.run?_id=467a55b506ba06b9ca364b1403880b65&_render=json&textinput1=40.78158&textinput2=-73.96648&_callback=?').success(barometer);
};
function barometer(data) {
console.log(data);
// 4. you had items instead of items[0]
// similar with data.
alert(data.value.items[0].data[1].parameters.pressure.value);
};
function showPrice(data) {
alert("Symbol: " + data.symbol[0] + ", Price: " + data.price);
}
* * 1。)**您必須在JSFiddle中選擇jQuery,而不是(默認)Mootools。 ** 2)**打開瀏覽器的JavaScript控制檯,查看發生的錯誤。 ** 3。)**您尚未定義回調函數! – ComFreek
我在第一個鏈接上得到了404。該頁面未找到 – KeatsKelleher
@akellehe他還在鏈接中忘記了「http://」。 – ComFreek