<script>
$(document).ready(function(){
$.ajax({
url:'http://localhost/test/jsonp.php',
dataType:"jsonp", //重点,使用jsonp方式解决跨域问题
jsonp:"jsonpcallback", //回调函数名,和php端统一
timeout: 5000,
success:function(data, status){
alert('success: ' + status);
var $ul = $("<ul></ul>");
$.each(data,function(i,v){
$("<li/>").text(v["id"] + " " + v["name"]).appendTo($ul)
});
$("#res").append($ul);
},
error:function(XHR, textStatus, errorThrown){
alert('error: ' + textStatus);
alert('error: ' + errorThrown);
}
});
});
</script>
$jsonp = $_GET['jsonpcallback'];
$arr = array(
'id' => '1',
'name' => 'test'
);
echo $jsonp, '([', json_encode($arr), '])'; //返回的数据中括号数组好像是必须的
?>
查看更多关于【Jquery】的文章