From my experience browser security does not seem to allow you to make jsonp calls by POSTING your vars. I have only got this to work with using GET parameters in the url
Here's a function in jquery that you can use to make your queries across domains:
PARAMETERS: var url - is a string of the actual URL you want to post to including parameters callbackFn is the actual function you want called on callback so no need to define a var, just put the function name in as a parameter - "NOT in a string"
SERVER SIDE:
**VERY IMPORTANT: You have to echo out the $_GET['callback']('value you want returned') on the server side
Example of test.php: <?php echo $_GET['callback'].'('.json_encode($myResult).')'; ?>
JAVASCRIPT / AJAX:
To run an API call just run:
var url = 'http://www.google.com/test.php';
function myFunc(msg){ alert(msg); }
function apiCall(url, callbackFn){ $.ajax({ type: "GET", url: url, dataType: "jsonp", success: function(msg){ callbackFn(msg); }, error: function(msg){ alert('Failed contacting server, please try again'); } }); }
apiCall(url,myFunc);
By PHPin24 @ 2010-02-12 17:19:21
|