Javascript Ajax:
<script type="text/javascript" src="http://www.MyFirstDomain.com/jquery.js"></script>
<script type="text/javascript">
$(document).ready( function () {
var referrer = document.referrer;
var currentpage = $(location).attr('href');
var data_object = {
'ref': referrer,
'curpg': currentpage,
'shizz1e': '21'
};
$.ajax({
'dataType': 'jsonp',
'jsonp': 'jsonp_callback',
'url': "http://www.myOtherDomain.com/ajaxreceiver.php",
'data': data_object
});
});
PHP:
<?php
$data = getDataAsJSON($_GET['curpg']);
echo $data;
?>
I’m trying to send some variables from Javascript on one server, to PHP on another. I’m using Ajax and JSONP (to work around the same origin policy). The websites I’ve used to try to understand JSONP have said to use getDataAsJSON() to get the JSON in PHP, but that apparently isn’t a real function. Am I going about this wrong?
in PHP you can use json_decode.
PHP doesn’t have a function called getDataAsJSON. THat is probably a custom function that someone wrote.
The function json_decode creates an object of the json string. So you won’t be able to echo it as it will give you an error. You can use print_r($data) to get the values of the object