Html 在JQuery中使用HTTPGET

Html 在JQuery中使用HTTPGET,html,jquery,Html,Jquery,我正在尝试使用GET HTTP cal,我得到了使用高级REST客户机()的请求,但无法使其在JQuery中工作 根据来自的建议,我建立了以下内容: $(document).ready(function() { $('.ap1').click(function(){ $.ajax({ url: 'https://api2.panopta.com/v2/monitoring_node/41', type: 'GET', dataType: 'json',

我正在尝试使用GET HTTP cal,我得到了使用高级REST客户机()的请求,但无法使其在JQuery中工作

根据来自的建议,我建立了以下内容:

$(document).ready(function() {
$('.ap1').click(function(){
$.ajax({
      url: 'https://api2.panopta.com/v2/monitoring_node/41',
      type: 'GET',
      dataType: 'json',
      success: function() { alert('hello!'); },
      error: function() { alert('boo!'); },
      beforeSend: setHeader
  });
});
function setHeader(xhr) {
    //extra stuff from REST CLIENT
    xhr.setRequestHeader('Authorization', 'ApiKey nGhhAjGshbOu4dhLYvGY');
} });
这是我试图获得的输出(使用REST客户端成功获得)

我只希望能够从JSON访问name变量,并通过我的函数传递它。在我试图找出如何创建一个对象之前,我想至少让上面的代码起作用,这样我就可以成功地调用这个名称,比如
.append(object.name)


我刚刚开始学习JQuery,这是我的第一篇文章,如果没有提供足够的详细信息,我感到非常抱歉。

您不能将ajax调用应用于其他域。您可以通过使用服务器到服务器调用进行变通,然后对脚本进行js调用

首先生成php文件,该文件将调用服务器注意,如果要使用file\u get\u内容,应在php.ini中允许\u url\u fopen:

myProxy.php

<?
$content = file_get_contents($yourUrl);
// do whatever you want with the content then
// echo the content or echo params via json
?>

我不确定您的api信息是否正确,但我认为您需要做的主要事情是更改为jsonp而不是json,因为musa提到了相同的源代码策略

以下JS FIDLE“工作”,但请求未在服务器上授权:


根据这一点,不可能使用带有自定义头(我的授权密钥)的JSONP,感谢您向我介绍JSFIDLE!
<?
$content = file_get_contents($yourUrl);
// do whatever you want with the content then
// echo the content or echo params via json
?>
$.ajax({
      url: 'myProxy.php',
      type: 'GET',
      dataType: 'json',
      success: function() { alert('hello!'); },
      error: function() { alert('boo!'); },
      beforeSend: setHeader
  });
});
$(document).ready(function() {
$('#button').click(function(){
$.ajax({
      url: 'https://api2.panopta.com/v2/monitoring_node/41',
      type: 'GET',
      dataType: 'jsonp',
      success: function() { alert('hello!'); },
      error: function() { console.log(arguments);alert('boo!'); },
      beforeSend: setHeader
  });
});
function setHeader(xhr) {
    //extra stuff from REST CLIENT
    xhr.setRequestHeader('Authorization', 'ApiKey nGhhAjGshbOu4dhLYvGY');
} });