JSONP传递api密钥

spu*_*der 2 ajax rest jsonp cosm xively

我有一个arduino上传传感器数据到cosm.com.我在我的本地Web服务器上创建了一个简单的网页来查询cosm.com API并打印出值.

问题是如果我没有在另一个标签中登录cosm.com,我会得到这个弹出窗口.

在此输入图像描述

解决方案是将我的公钥传递给cosm.com,但我在这里已经超出了我的想法.

该文档提供了如何在curl中执行此操作的示例,但不是javascript

curl --request GET --header "X-ApiKey: -Ux_JTwgP-8pje981acMa5811-mSAKxpR3VRUHRFQ3RBUT0g" https://api.cosm.com/v2/feeds/120687/datastreams/sensor_reading
Run Code Online (Sandbox Code Playgroud)


如何将我的密钥传递到网址?:

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'
});
}
Run Code Online (Sandbox Code Playgroud)


更新: 必须是可能的,因为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/

the*_*ejh 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'
  });
}
Run Code Online (Sandbox Code Playgroud)