3

I'm trying to use a GET HTTP cal, I got the request to work with the Advanced REST client (Chrome plugin) but cannot get it to work in JQuery.

Following the advice from this thread, I've set up the following:

$(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');
} });

This is the output I'm trying to get (successfully with REST client)

{
url: "https://api2.panopta.com/v2/monitoring_node/41"
hostname: "phoenix01.monitorengine.com"
ip_address: "65.23.158.149"
name: "Phoenix"
textkey: "na.west.phoenix01"
}

I just want to be able to access the name variable from that JSON and pass it through my function. I wanted to at least get the above code to work before I try to figure out how to create an object so I can successfully call the name, something like .append(object.name)

I just started learning JQuery and this is my first post so sorry if I didn't include enough details.

Community
  • 1
  • 1
Ali Bhujwala
  • 217
  • 2
  • 8

2 Answers2

1

You can't apply ajax calls to other domains. You can make workaround using server-to-server calls via curl() or file_get_content(url), and then make a js call to your script.

First make php file, which will call the server note that if you want to use file_get_contents you should allow_url_fopen in your php.ini:

myProxy.php

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

your js should make call to your PHP, so you have workaround Same Domain Policy:

$.ajax({
      url: 'myProxy.php',
      type: 'GET',
      dataType: 'json',
      success: function() { alert('hello!'); },
      error: function() { alert('boo!'); },
      beforeSend: setHeader
  });
});
bksi
  • 1,543
  • 1
  • 22
  • 39
0

I am not sure if your api info is correct, but I think the main thing you need to do is change to jsonp instead of json due to the same origin policy that musa mentions.

The following JS fiddle "works" but the request is not authorized at the server: http://jsfiddle.net/cf8S9/.

$(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');
} });
sasbury
  • 301
  • 1
  • 4
  • It's impossible to use JSONP with custom headers(my authorization key) according to this [question](http://stackoverflow.com/questions/3073287/set-headers-with-jquery-ajax-and-jsonp), thanks for introducing me to jsfiddle! – Ali Bhujwala Jun 19 '13 at 18:27