分享

jQuery Ajax jsonp实现跨域请求(jQuery给出的默认方法)

 明天网吧 2015-07-17

<!--html 代码清单:

[html] -->


*/

<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<script src="jquery-1.7.min.js"></script>


<script type="text/javascript">
$(function(){
$.ajax({data:{"code":200},
  type: "get",
    url:"http://ttzz.:9090/b.php",
  dataType: "jsonp",
   jsonp:"call",
  //jsonpCallback:"fn",   //这行注释掉了,使用jQuery给出的默认方法
   success:function(res){//res是json类型;
   alert(res.name);
   }
});
});
//function fn(data){alert(data);}
</script>
</head>

<body>
</body>
</html>

**********服务端b.php 代码清单:*********

*********[php] *********************************/

<?php
header('Content-Type:text/html;Charset=utf-8'); 
$code = $_GET['code'];  //传过来的数据

$call = $_GET['call'];  //传过来的函数名,有可能是自定义,也有可能是jQery系统定义的。
$json_data='{"name":"li"}'; //正确的写法
// 正确的写法  $json_data='"zifuchang"';  单引号里面是双引号,最里面是字符
//错误的写法  $json_data="zifuchang";    正常的字符串写法,错误!
$json_data='{"name":"li"}';
  $res=$call. "(".$json_data.")";  

echo $res;  
?>



    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多