码迷,mamicode.com
首页 > Web开发 > 详细

jsonp 原理

时间:2014-11-22 22:55:37      阅读:611      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   sp   java   

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>

<body>
<script>
function fn(data){
    alert(data)    
};
</script>

<script src="data.js"></script>      
</body>
</html>

data.js 的内容是 fn([1,2,3,4,5])

输出结果:

bubuko.com,布布扣

实例:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<script src="jquery1.8.3.min.js"></script>
</head>
<body>
<dl id="Dl1">
	
</dl>
<script>
function fn(data) {
	var oDl1 = $(‘#Dl1‘);
	var html = ‘‘;
	$.each(data,function(index,content){
		$.each(content,function(i,c){
			html += ‘<dt>‘+c[‘title‘]+‘</dt>‘;
			html += ‘<dd>‘+c[‘summary‘]+‘</dd>‘;
		})	
	})
	oDl1.html(html)
};
var oScript = document.createElement(‘script‘);
oScript.src = ‘https://api.douban.com/v2/book/search?q=javascript&count=10&callback=fn‘;
document.body.appendChild(oScript);
document.body.removeChild(oScript);
</script>
</body>
</html>

处理数据:

//遍历数组 第一个参数是索引 第二个是内容
$.each( [0,1,2], function(i, n){
  console.log( "索引:" + i + "   内容: " + n );
});
//遍历对象 第一个参数是key 第二个参数value
$.each( {name:"liujin","age":"24","sex":"boy"}, function(i, n){
  console.log( "key:"+ i + "    value: " + n );
});

bubuko.com,布布扣

jsonp 原理

标签:style   blog   http   io   ar   color   os   sp   java   

原文地址:http://www.cnblogs.com/liujin0505/p/4115731.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!