9

I've been trying to make an AJAX request to an external server. I've learned so far that I need to use getJSON to do this because of security reasons ?

Now, I can't seem to make a simple call to an external page. I've tried to simplify it down as much as I can but it's still not working. I have 2 files, test.html & test.php

my test.html makes a call like this, to localhost for testing :

    $.getJSON("http://localhost/OutVoice/services/test.php", function(json){
    alert("JSON Data: " + json);
});

and I want my test.php to return a simple 'test' :

$results = "test";
echo json_encode($results);

I'm probably making some incredible rookie mistake but I can't seem to figure it out. Also, if this works, how can I send data to my test.php page, like you would do like test.php?id=15 ?


The test.html page is calling the test.php page on localhost, same directory I don't get any errors, just no alert ..

Shiladitya
  • 12,003
  • 15
  • 25
  • 38
Pmarcoen
  • 1,216
  • 4
  • 20
  • 33
  • Whats the page thats making the request. I want to make sure you're not having a cross-domain security issue. – bendewey Apr 26 '09 at 14:20
  • The test.html page is calling the test.php page on localhost, same directory I don't get any errors, just no alert .. – Pmarcoen Apr 26 '09 at 14:27
  • Perhaps you need to specify content-type: header('Content-Type: application/json'); echo json_encode($results); – rojoca Apr 26 '09 at 20:40

3 Answers3

17

It could be that you haven't got a callback in test.php. Also, json_encode only accepts an array:

$results = array("key" => "value");
echo $_GET['callback'] . '(' . json_encode($results) . ')';
// the callback stuff is only needed if you're requesting from different domains

jQuery automatically switches to JSONP (i.e. using script tags instead of XMLHttpRequest) when you use http://. If you have test.html and test.php on the same domain, try using relative paths (and no callbacks).

moff
  • 6,415
  • 31
  • 30
  • 1
    +1. I'm about to implement callbacks in the server-side as well, but if I do choose to host client and server on the same domain, will still using a callback break it? (I'll probably be able to answer my own question in about an hour or so...) – opyate Oct 06 '09 at 14:52
  • A callback will always work, even if you request the file from the same domain. It's also proven to be faster to use a JSON callback than requesting and eval'ing the object (since eval is so slow). The Yahoo Flickr team wrote about this a few months ago: http://code.flickr.com/blog/2009/03/18/building-fast-client-side-searches/ – moff Oct 08 '09 at 12:24
  • 2
    That totally opens you up to an XSS venerability - http://www.metaltoad.com/blog/using-jsonp-safely – kajo May 03 '12 at 16:42
15

Be careful with moff's answer. It's got a common XSS vulnerability: http://www.metaltoad.com/blog/using-jsonp-safely

drewish
  • 9,042
  • 9
  • 38
  • 51
2

The simplest solution would be to add the below code before any output to your test.php file, then you have more flexibility with what methods you use, a standard ajax call should work.

header ('Access-Control-Allow-Origin: *');

However, use the json callback thing when your getting data from a server beyond your control.

Ally
  • 4,894
  • 8
  • 37
  • 45
  • +1, but you should really restrict it to the server(s) you're requesting from. Also, IE support for this is lagging, so if you need browser support, better to go with the JSONP method. – Patrick M Jul 18 '12 at 23:55