Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/403.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用JQuery.Ajax获取Pingdom检查?_Javascript_Jquery_Ajax_Api_Pingdom - Fatal编程技术网

Javascript 如何使用JQuery.Ajax获取Pingdom检查?

Javascript 如何使用JQuery.Ajax获取Pingdom检查?,javascript,jquery,ajax,api,pingdom,Javascript,Jquery,Ajax,Api,Pingdom,据我所知,我的问题是我的GET请求未经授权。但我尝试在标题中添加授权或在URL中添加值(api密钥、用户名、密码)的尝试没有成功 例如 有人能建议如何更正与PingDomAPI交互的Javascript语法吗?我认为我试图错误地授权他们的文档集中在PHP上,而我在这种情况下无法使用PHP 我认为不可能在web浏览器中使用Javascript中的PingDomAPI 您需要使用jsonp来让浏览器允许跨站点的ajax请求,但根据这一点,在jsonp请求中设置头是不可能的。在任何地方使用CORS。

据我所知,我的问题是我的GET请求未经授权。但我尝试在标题中添加授权或在URL中添加值(api密钥、用户名、密码)的尝试没有成功

例如

有人能建议如何更正与PingDomAPI交互的Javascript语法吗?我认为我试图错误地授权他们的文档集中在PHP上,而我在这种情况下无法使用PHP


我认为不可能在web浏览器中使用Javascript中的PingDomAPI

您需要使用jsonp来让浏览器允许跨站点的ajax请求,但根据这一点,在jsonp请求中设置头是不可能的。

在任何地方使用CORS。 我想得到一个简单的jQuery请求,用于检查我们平台的最后一个Pingdom结果。由于CORS和需要为身份验证指定自定义头,这似乎是不可能的

我不想为这么简单的东西设置代理服务器,所以我找到了,并能够使用CORS Anywhere方法,看起来像这样:

// Will use the cors-anywhere proxy if jQuery supports it and crossDomain option is passed in.
$.ajaxPrefilter( function (options) {
  if (options.crossDomain && jQuery.support.cors) {
    var http = (window.location.protocol === 'http:' ? 'http:' : 'https:');
    options.url = http + '//cors-anywhere.herokuapp.com/' + options.url;
    // options.url = "http://cors.corsproxy.io/url=" + options.url;
  }
});

// Use ajax requests as normal.
$.ajax({
  type: 'get',
  async:   false,
  crossDomain: true,
  beforeSend: function(xhr){
    xhr.setRequestHeader('Authorization', 'Basic encodedusername:passwordhere');
  },
  url: "https://api.pingdom.com/api/2.0/checks",
  success: function(Data) {
    console.log(Data);
  },
    error: function(Data) { 
  }
});

注意:如果传递或检索机密信息,请不要使用此选项。如果传递或检索机密信息,请使用自己的代理。但是,如果您只是像我们一样获取公共数据,那么这应该是一种绕过CORS限制的好方法。

据我所知,您应该提供的不是encodeBase64(登录)+“:”+encodeBase64(通过),而是encodeBase64(登录+”:“+通过);此外,看起来您的数据提供商需要另一个应用程序密钥;一步一步:async:false是一个非常糟糕的做法。这就是我正在使用的。对不起,我想用我用过的占位符很难弄清楚。只返回“加载资源失败:服务器响应状态为401(未经授权)”,我确信用户和密码正确,因为导航到弹出窗口并在弹出窗口中输入它们可以正常工作。请确保您也发送应用程序密钥标题。并尝试使用一些web调试器(我推荐fiddler,但也可以使用chrome的webinspector)来检查您的请求是否确实包含所需的标题。啊,有趣的是,添加
xhr.setRequestHeader('App-Key','myapikeyhere')返回类似但不同的错误
选项https://api.pingdom.com/api/2.0/checks 401(未经授权)
我现在一定会查看fiddler和webinspector。
// Will use the cors-anywhere proxy if jQuery supports it and crossDomain option is passed in.
$.ajaxPrefilter( function (options) {
  if (options.crossDomain && jQuery.support.cors) {
    var http = (window.location.protocol === 'http:' ? 'http:' : 'https:');
    options.url = http + '//cors-anywhere.herokuapp.com/' + options.url;
    // options.url = "http://cors.corsproxy.io/url=" + options.url;
  }
});

// Use ajax requests as normal.
$.ajax({
  type: 'get',
  async:   false,
  crossDomain: true,
  beforeSend: function(xhr){
    xhr.setRequestHeader('Authorization', 'Basic encodedusername:passwordhere');
  },
  url: "https://api.pingdom.com/api/2.0/checks",
  success: function(Data) {
    console.log(Data);
  },
    error: function(Data) { 
  }
});