2013-03-31 71 views
2

我有一個arduino上傳傳感器數據到cosm.com。我在本地Web服務器上創建了一個簡單的網頁,以查詢cosm.com API並打印出值。JSONP傳遞api密鑰

問題是,如果我沒有在另一個選項卡中登錄到cosm.com,我會看到這個彈出窗口。

enter image description here

的解決方案是我的公共密鑰傳遞給cosm.com,但我在一路過來我這裏頭。

文檔給出瞭如何做到這一點的捲曲的例子,但不是JavaScript的

curl --request GET --header "X-ApiKey: -Ux_JTwgP-8pje981acMa5811-mSAKxpR3VRUHRFQ3RBUT0g" https://api.cosm.com/v2/feeds/120687/datastreams/sensor_reading 


我如何通過我的鑰匙插入網址?:

function getJson() { 
$.ajax({ 
    type:'GET', 
    url:"https://api.cosm.com/v2/feeds/120687/datastreams/sensor_reading", 

//This line isn't working 
    data:"X-ApiKey: -Ux_JTwgP-8pje981acMa5811-mSAKxpR3VRUHRFQ3RBUT0g", 

    success:function(feed) { 

    var currentSensorValue = feed.current_value; 
     $('#rawData').html(currentSensorValue); 
    }, 
    dataType:'jsonp' 
}); 
} 


更新: 它必須是可能的因爲hurl.it能夠查詢的API http://www.hurl.it/hurls/75502ac851ebc7e195aa26c62718f58fecc4a341/47ad3b36639001c3a663e716ccdf3840352645f1

更新2: 雖然我從來沒有得到這個工作,我沒有找到周圍工作。 Cosm擁有自己的JavaScript庫,可以滿足我的需求。

http://cosm.github.com/cosm-js/ http://jsfiddle.net/spuder/nvxQ2/5/

回答

0

它應該是更容易得到它使用CosmJS工作。它是一個官方支持的庫,提供Cosm API的全面覆蓋。

+0

謝謝!這就是我最終做的。 https://github.com/spudstud/Foosball – spuder

5

你需要把它發送一個報頭,而不是作爲查詢字符串,那麼試試這個:

function getJson() { 
    $.ajax({ 
    type:'GET', 
    url:"https://api.cosm.com/v2/feeds/120687/datastreams/sensor_reading", 
    headers:{"X-ApiKey": "-Ux_JTwgP-8pje981acMa5811-mSAKxpR3VRUHRFQ3RBUT0g"}, 
    success:function(feed) { 
    var currentSensorValue = feed.current_value; 
     $('#rawData').html(currentSensorValue); 
    }, 
    dataType:'jsonp' 
    }); 
} 
+0

感謝這看起來非常接近。我仍然沒有工作。 http://jsfiddle.net/spuder/Bsg7E/10/ – spuder

+0

@spuder也許API不會發送允許您發送自定義標頭所必需的標頭。 – thejh

+0

雖然這在我的情況下不起作用,但這確實解釋了原理。我正在回答。 – spuder