<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<script type="text/javascript">
function do_jsonp() {
$.getJSON("http://ceshi.cc/profile2.php?callback=?",
// http://ceshi.cc/profile2.php
function(data) {
//console.log('afdf');
$('#result').val('My name is:' +data.contact.MSN);
//$('#result').val('My name is:' +data.name);
});
}
</script>
<a href="javascript:do_jsonp();">Click me</a><br />
<textarea id="result" cols="50" rows="3"></textarea>
</body>
</html>
<?php
$callback = isset($_GET['callback']) ? $_GET['callback'] : '';
$json = '';
//php数组
$arr = array(
'name' => 'lava',
'nick' => '比目鱼',
'contact' => array(
'MSN' => 'LIUXIOAJUN@MSN.COM',
)
);
//echo json_encode($arr);
$arr = gb2312ToUtf8($arr);//中文需要转UTF-8
$json = json_encode($arr);//转成json数组
if (!empty($callback)) {
$json = $callback . '(' . $json . ')';//注意这里的格式,调试时这里费了点时间
}
echo $json;
function gb2312ToUtf8($input)
{
if (!is_array($input)) {
//$input = iconv('GB2312', 'UTF-8', $input);
$input = iconv('utf-8','gbk//IGNORE',$input);
} else {
foreach ($input as $k=>$v) {
gb2312ToUtf8($input["$k"]);
}
}
return $input;
}
?>