跨域的问题
- 域:域名
- 跨域请求(访问):一个域名下的文件请求另外一个域名下的资源,就产生了跨域
- 跨域的解决:
- Jsonp:json with padding
src的作用:加载(包含指定的外部文件)
可以跨域包含
被包含的资源可以是任何类型的文件(可以是txt,png,jpg)
他只关注被包含的文件的内容是否是合法的js
-
- 定义函数
- 包含外部文件,在被包含的文件中执行调用定义好的函数
- 参数的(数据)的实现
- 问题:包含就调用,通过动态创建<script>实现按需调用
- 问题:包含动态文件时可以通过一个接口实现按需生成调用函数名称
-
- 实例
- 百度搜索下拉提示
- 豆瓣接口调用
- 实例
JSONP基本原理
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<script>
//方法必须写在外面要不然就用window[xxx]因为调用的时候必须是window
function fn1(data) {
var oUl1 = document.getElementById('ul1');
var html = '';
for (var i=0; i<data.length; i++) {
html += '<li>'+data[i]+'</li>';
}
oUl1.innerHTML = html;
}
function fn2(data) {
var oUl2 = document.getElementById('ul2');
var html = '';
for (var i=0; i<data.length; i++) {
html += '<li>'+data[i]+'</li>';
}
oUl2.innerHTML = html;
}
function fn3(data) {
var oUl3 = document.getElementById('ul3');
var html = '';
for (var i=0; i<data.length; i++) {
html += '<li>'+data[i]+'</li>';
}
oUl3.innerHTML = html;
}
</script>
<script>
window.onload = function() {
var oBtn1 = document.getElementById('btn1');
var oBtn2 = document.getElementById('btn2');
oBtn1.onclick = function() {
var oScript = document.createElement('script');
oScript.src = 'getData.php?callback=fn1';
document.body.appendChild(oScript);
}
var oBtn2 = document.getElementById('btn2');
oBtn2.onclick = function() {
var oScript = document.createElement('script');
oScript.src = 'getData.php?t=str&callback=fn2';
document.body.appendChild(oScript);
}
var oBtn3 = document.getElementById('btn3');
oBtn3.onclick = function() {
var oScript = document.createElement('script');
oScript.src = 'getData.php?callback=fn3';
document.body.appendChild(oScript);
}
}
</script>
</head>
<body>
<input type="button" id="btn1" value="加载数字" />
<ul id="ul1"></ul>
<input type="button" id="btn2" value="加载字母" />
<ul id="ul2"></ul>
<input type="button" id="btn3" value="加载字母" />
<ul id="ul3"></ul>
</body>
</html>
getData.php里面的代码
<?php
$t = isset($_GET['t']) ? $_GET['t'] : 'num';
$callback = isset($_GET['callback']) ? $_GET['callback'] : 'fn1';
$arr1 = array('111111','22222222','33333333','4444444','555555555555555555555');
$arr2 = array('aaaaaaaaaaaa','bbbbbbbb','cccccccccccc','ddddddddd','eeeeeeeeeeee');
if ($t == 'num') {
$data = json_encode($arr1);
} else {
$data = json_encode($arr2);
}
echo $callback.'('.$data.');';
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv='Content-Type' content="text/html; charset=utf-8">
<meta http-equiv='X-UA-Compatible' content='IE=edge,chrome=1'>
<title>仿造百度下拉</title>
<script src="js/ajaxbaidu.js"></script> //调用了自己写的ajax类库 修改了里面的callback
</head>
<style>
#btn {width: 300px; height: 30px; padding: 5px; border:1px solid #f90; font-size: 16px;}
#ul1 {border:1px solid #f90; width: 310px; margin: 0;padding: 0; display: none;}
li a { line-height: 30px; padding: 5px; text-decoration: none; color: black; display: block;}
li a:hover{ background: #f90; color: white; }
</style>
<body>
<input type="text" id="btn" />
<ul id="ul1"></ul>
</body>
<script>
var btnId = document.getElementById("btn"); //获取到button
var ul1 = document.getElementById("ul1"); //获取到ul1
btnId.onkeyup = function(){
ajax({
type:"get",
url: "http://suggestion.baidu.com/su?wd="+this.value,
dataType:"jsonp",
success:function(data){
var dataall = data.s;
var html = "";
if(dataall.length)
{
for(var i=0;i<dataall.length;i++)
{
html += '<li><a href="#">'+dataall[i]+'</a></li>';
}
ul1.style["display"] = "block";
ul1.innerHTML = html;
}else
{
ul1.style["display"] = "none";
}
}
})
}
</script>
</html>
豆瓣
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv='Content-Type' content="text/html; charset=utf-8">
<meta http-equiv='X-UA-Compatible' content='IE=edge,chrome=1'>
<title>刷豆瓣下拉</title>
<script src="js/ajaxdouban.js"></script>
<style>
*{margin:0px;padding:0px;}
#q {width: 300px; height: 30px; padding: 5px; border:1px solid #f90; font-size: 16px;}
dl {border-bottom: 1px dotted #000;}
dt {font-weight: bold;}
</style>
<body>
http://www.douban.com/service/apidoc/reference/
<input type="text" id="q" /><input type="button" id="btn" value="搜索" />
<p id="msg"></p>
<hr />
<div id="list"></div>
</body>
<script>
document.getElementById("btn").onclick = function(){
var value = document.getElementById("q").value;
ajax({
type: "get",
dataType : "jsonp",
url: 'http://api.douban.com/book/subjects?q='+value+'&alt=xd',
success:function(data){
var oMsg = document.getElementById('msg');
var oList = document.getElementById('list');
console.log(data);
oMsg.innerHTML = data.title.$t + ' : ' + data['opensearch:totalResults'].$t;
var aEntry = data.entry;
var html = '';
for (var i=0; i<aEntry.length; i++) {
html += '<dl><dt>'+ aEntry[i].title.$t +'</dt><dd>![]('+ aEntry[i].link[2]['@href'] +')</dd></dl>';
}
oList.innerHTML = html;
}
})
}
</script>
</html>