2013-03-31 72 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