本地:
<?php $_arr = array('a'=>1,'b'=>2,'c'=>3); $_result = json_encode($_arr); echo $_result; ?>
//本地获取 $('form input[type=button]').click(function(){ $.ajax({ type:"post", url:"test.php", async:true, dataType:'json', success:function(response,status,xhr){ alert(response.a); } }); });
跨域获取:假如在www.dang.cc上有一个test2.php文件如下
<?php $_arr = array('a'=>1,'b'=>2,'c'=>3); $_result = json_encode($_arr); $_callback = $_GET['callback']; echo $_callback."($_result)"; ?>
//远程获取 $('form input[type=button]').click(function(){ $.ajax({ type:"post", url:"http://www.dang.cc/test2.php", async:true, dataType:'jsonp', success:function(response,status,xhr){ alert(response.a); } }); });