开发者

jsonp simple call not working

i have this simple 2 files that should process simple jsonp call

here is the开发者_如何学编程 index file :

<html>
<head>
    <script type="text/javascript" id="myJSONPCall" src="http://mySubDomain.comoj.com/jsoncall.php?jsonCallback=myCallback"></script>
    <script type="text/javascript">
        function myCallback(obj) {
            alert(obj.text);
        }
    </script>
</head>

<body>
</body>
</html>

and here is the php part :

<?php   
$myObject = array(
    "text" => "Hello, I am data from the remote server.",
    "created_at" => "Thu May 07 21:36:12 +0000 2009"
);

$myJSONObject  = json_encode($myObject);

$myJSONCallback = filter_var($_REQUEST['jsonCallback'], FILTER_SANITIZE_STRING);

print "$myJSONCallback($myJSONObject)"
?>

here , i update it. what is wrong here? what i exactly i did wrong?


Well, where do you suppose your GET number to be printed? When your browser request the http://mysubdomain.comoj.com/jsoncall.php?jsonCallback=3 url, it's make an HTTP request to the php page. When the http server send you a response it just contain "3" as body, wich is not valid javascript and nothing happen.

Moreover, if you want to create a php page that build a dinamic javascript you have to first tell the browser that you're sending a javascript file by using the header("Content-Type: textt/javascript"); directive before print anything to screen.

Last but not least...you're not doing a jSONP call at all :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜