Getting JSON callback function to fire when page loads

They have: 426 posts

Joined: Feb 2005

I am generating some JSON using php function json_encode. This is all fine but when I call this page and try and put the callback function in the src URL the callback function does not fire when page has loaded?

I have read a few articles and it looks like I actually need to set up a kind of JSON webservice or something.

Can anyone give me some pointers or advice on how to get this to work?

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

It's not really a web service. It's just a PHP script that returns JSON instead of HTML.

All you need to do is do an AJAX postback and process the returned JSON. This is the best AJAX example w/o using a framework like jQuery:

http://www.w3schools.com/PHP/php_ajax_database.asp

They have: 426 posts

Joined: Feb 2005

This is not exactly what I mean: Basically, if I have a normal HTML webpage with a script tag in the head or body:

<script src="http://php-page-that-generates-JSON/?param=1&callback=callbackfunction"></script>

then I create a function with the same name as the callback parametre ("function callbackfunction()") then this function automatically gets fired once the page has loaded.

EDIT: actually it does work I just need to define the function before including the json file!

If you look at the Yahoo webservice, this is what it does:

http://developer.yahoo.com/common/json.html#callbackparam

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.