javascript - JSONP - How The Heck Do I Use It? -
been trying cook jsonp around cross domain issues. used answer here: basic example of using .ajax() jsonp?
$.getjson("http://example.com/something.json?callback=?", function(result){ //response data in result variable alert(result); });
but not getting desired result. code:
jquery
var url = "http://wplivestats.com/stats/jsonp.php?callback=?"; $.getjson(url, function(result){ //response data in result variable console.log(result); });
php
<?php include('init.php'); $pubid = $_request['pid']; date_default_timezone_set ( 'america/new_york' ); $time = date('y-m-d h:i:s',time()-$polling_minutes*60); $count = $conn->prepare("select distinct ip stats timestamp >= '$time' , pubid = '$pubid'"); $count->execute(); $users['users'] = $count->rowcount(); echo "jsoncallback ( ["; echo json_encode($users); echo "] )"; ?>
the error:
referenceerror: jsoncallback not defined jsoncallback ( [{"users":0}] )
where going wrong?
the problem in php script. when request using jquery question mark in url replaced dynamic function name.
on php side need use dynamic function name wrap data instead of using "jsoncallback".
your php code should this:
echo $_get['callback'] . "(" . json_encode($users) . ")";
Comments
Post a Comment