jquery ajax 跨域
2016-05-31 11:40
585 查看
1、jsonp
html<script type="text/javascript" src="http://www.youxiaju.com/js/jquery-1.4.2.min.js"></script> <script type="text/javascript"> $(function(){ $.ajax( { type:'get', url : 'http://www.youxiaju.com/validate.php?loginuser=lee&loginpass=123456', dataType : 'jsonp', jsonp:"jsoncallback", success : function(data) { alert("用户名:"+ data.user +" 密码:"+ data.pass); }, error : function() { alert('fail'); } } ); }) </script>
php
<?php header('Content-Type:text/html;Charset=utf-8'); $arr = array( "user" => $_GET['loginuser'], "pass" => $_GET['loginpass'], "name" => 'response' ); echo $_GET['jsoncallback'] . "(".json_encode($arr).")";
**
2、getJosn
**$.getJSON("http://127.0.0.1/server/index.php/jsonp/getJson?callback=?", { id: "123456", site: "01" }, function(data) { console.log(data) alert(data.id + ',' + data.site + ',' + data.name); });
php
$get = $this->input->get(); $id = $get['id']; $site = $get['site']; $arr = array( "id" =>$id, "site" => $site, "name" => 'response' ); echo $_GET['callback'] . "(".json_encode($arr).")";
参考:
参考文章
jsonp原理
相关文章推荐
- JQuery1——基础($对象,选择器,对象转换)
- JavaScript演示排序算法
- 2015-2016网页设计趋势分析 Web Design of Trends
- jQuery Ajax 跨域调用
- 移动端的长按事件
- jquery教程靠边站,一分钱不花让你免费学会jquery
- JQuery+Strusts1.x无刷新登录
- Ajax
- $.ajax()方法详解
- jQuery ajax - ajax() 方法
- 使用Ajax实现异步用户名验证
- 使用ajax实现用户登录验证(升级版)
- JavaScript 各种遍历方式详解
- 数组方法汇总
- 解决Ajax悬停效果,无法遮蔽FLASH的问题
- 再谈Jquery Ajax方法传递到action(补充)
- jQuery Html控件基本操作(日常收集整理)
- jQuery插件实现文字无缝向上滚动效果代码