跨域之jsonp

时间:2023-11-24 14:54:44

我们都知道使用<script>标签可以引入外部的JS文件,即使这个JS文件来自于其他的网站,比如我们引用存放在网络服务器上的jQuery框架。在这个过程中,我们已经实现跨域访问。像<script>标签这种本身具有跨域访问能力的标签还有<link>、<img>、<iframe>等。jsonp的实现原理就是利用<script>标签,实现了跨域访问。

先看一个例子:

先创建一个html文件,代码如下:

 <body>
<p>名字:<span id="name"></span></p>
<script>
function test(obj) {
document.getElementById("name").innerHTML = obj.name;
}
</script>
</body>

此时在浏览器打开这个文件,页面中只有“名字:”这三个字符。

现在创建一个JS文件,test.js,其中的代码如下:

 var person = {
name: "Jack",
age: 20
}
test(person);

通过<script>标签在html文件中引入这个test.js文件(添加到body结束标签之前):

 <script src="test.js"></script>

再在浏览器中打开html文件就会看到:

跨域之jsonp

如果把这个test.js放在百度的服务器上,在淘宝的页面上有一个test函数声明,然后动态地创建一个<script>标签来引用这个test.js,那么在淘宝的页面上就可以显示来自百度的服务器的数据,这就是jsonp的原理。当然引用的不一定非要是js文件,也可以是php、.net等文件,只要在这些文件中事先写好相应的代码即可。

下面用聚合数据的天气预报接口和新浪的城市查询接口为例来介绍jsonp。

首先是新浪的城市查询接口。

新浪的城市查询接口是:

http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js

可以直接在浏览器中打开,发现是一个名为remote_ip_info的Javascript对象,

var remote_ip_info = {
"ret":1,
"start":-1,
"end":-1,
"country":"\u4e2d\u56fd",
"province":"\u6cb3\u5357",
"city":"\u8bb8\u660c",
"district":"",
"isp":"",
"type":"",
"desc":""
};

那么在本地的测试页面中可以使用这个接口获取所在的城市,代码如下:

 //获取当前城市
function getCity(url) {
var city = '';
//创建一个script标签
var script = document.createElement("script");
//设置script标签引用的url
script.setAttribute("src",url);
//将script标签添加到头部,也可以添加到其他地方
var head = document.getElementsByTagName('head')[0];
head.appendChild(script);
//如果script标签加载完成,获取城市
script.onload = function() {//script.onload不兼容IE8及以下版本
city = remote_ip_info["city"];
console.log(city);
}; //移除创建的script标签
head.removeChild(script);
} //该URL是新浪的获取当前城市的API,采用jasop方法获取数据
getCity("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js");

下面是聚合数据的天气预报接口。

接口示例:http://op.juhe.cn/onebox/weather/query?cityname=%E6%B8%A9%E5%B7%9E&key=您申请的KEY

使用这个接口时至少要传入三个参数:

  1. cityname是要查询的城市名,可以用前面新浪的城市查询接口得到的数据。
  2. key是申请使用聚合数据天气预报接口时,给的APPKey。
  3. 另外还有一个callback参数,传入一个函数名,服务器会把查询后得到的数据拼接成函数调用的形式返回来,比如传入test函数名,则返回来test({"date":"2011-11-11","wind":"微风"}),所以要在测试页面声明好这个函数,以便对返回来的数据进行处理。

下面是代码:

html代码,将获得数据在页面中显示出来

 <p id="chuanyi"></p>
<p id="yundong"></p>
<p id="ziwaixian"></p>

获得天气情况数据的代码,原理与获取当前城市的原理一样,不同的是要事先指定回调函数,并将函数名加到url的末尾。

 function $(id) {
return document.getElementById(id);
} //指定回调函数
function callback(data) {
$("chuanyi").innerHTML = "穿衣指数:" + data.result.data.life.info.chuanyi;
$("yundong").innerHTML = "运动指数:" + data.result.data.life.info.yundong;
$("ziwaixian").innerHTML = "紫外线:" + data.result.data.life.info.ziwaixian;
} function getWeather(city) {
var url = 'http://op.juhe.cn/onebox/weather/query?cityname='+encodeURIComponent(city)+'&key=abbac0fc4fccbec34891de77bb1cfb4e&callback=callback';//该URL是聚合数据的天气预报API,采用jasop方法获取数据
console.log(encodeURIComponent(city));
var script = document.createElement("script");
script.setAttribute("src",url);
var head = document.getElementsByTagName('head')[0];
head.appendChild(script);
head.removeChild(script);
}

有了以上代码,还不能获取到天气数据,因为getWeather()函数还没有调用。为了能将获取到的城市数据传给getWeather()函数,要在getCity()函数内部script的onload事件处理程序中调用getWeather()函数。

下面是调用getWeather函数返回的数据,一大串:

 callback({
"reason":"successed!",
"result":{
"data":{
"pubdate":"2016-11-11",
"pubtime":"11:00:00",
"realtime":{
"city_code":"101180401",
"city_name":"许昌",
"date":"2016-11-11",
"time":"16:00:00",
"week":5,
"moon":"十月十二",
"dataUptime":1478853365,
"weather":{
"temperature":"15",
"humidity":"64",
"info":"多云",
"img":"1"
},
"wind":{
"direct":"南风",
"power":"3级",
"offset":null,
"windspeed":null
}
},
"life":{
"date":"2016-11-11",
"info":{
"chuanyi":["较冷","建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。"],
"ganmao":["较易发","天凉,昼夜温差较大,较易发生感冒,请适当增减衣服,体质较弱的朋友请注意适当防护。"],
"kongtiao":["较少开启","您将感到很舒适,一般不需要开启空调。"],
"xiche":["较适宜","较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。"],
"yundong":["较适宜","天气较好,但考虑气温较低,推荐您进行室内运动,若户外适当增减衣物并注意防晒。"],
"ziwaixian":["弱","紫外线强度较弱,建议出门前涂擦SPF在12-15之间、PA+的防晒护肤品。"]
}
},
"weather":[
{
"date":"2016-11-11",
"info":{
"day":["1","多云","17","","微风","06:53"],
"night":["0","晴","7","","微风","17:23"]
},
"week":"五",
"nongli":"十月十二"
},
{
"date":"2016-11-12",
"info":{
"dawn":["0","晴","7","无持续风向","微风","17:23"],
"day":["0","晴","17","","微风","06:54"],
"night":["1","多云","8","","微风","17:23"]
},
"week":"六",
"nongli":"十月十三"
},
{
"date":"2016-11-13",
"info":{
"dawn":["1","多云","8","无持续风向","微风","17:23"],
"day":["1","多云","17","","微风","06:55"],
"night":["0","晴","9","","微风","17:22"]
},
"week":"日",
"nongli":"十月十四"
},
{
"date":"2016-11-14",
"info":{
"dawn":["0","晴","9","无持续风向","微风","17:22"],
"day":["1","多云","17","","微风","06:56"],
"night":["2","阴","5","","微风","17:21"]
},
"week":"一",
"nongli":"十月十五"
},
{
"date":"2016-11-15",
"info":{
"dawn":["2","阴","5","无持续风向","微风","17:21"],
"day":["1","多云","13","","微风","06:57"],
"night":["1","多云","6","","微风","17:21"]
},
"week":"二",
"nongli":"十月十六"
}
],
"f3h":{
"temperature":[
{
"jg":"20161111170000",
"jb":"15"
},
{
"jg":"20161111200000",
"jb":"13"
},
{
"jg":"20161111230000",
"jb":"10"
},
{
"jg":"20161112020000",
"jb":"10"
},
{
"jg":"20161112050000",
"jb":"8"
},
{
"jg":"20161112080000",
"jb":"7"
},
{
"jg":"20161112110000",
"jb":"14"
},
{
"jg":"20161112140000",
"jb":"16"
},
{
"jg":"20161112170000",
"jb":"14"
}
],
"precipitation":[
{"jg":"20161111170000","jf":"0"},
{"jg":"20161111200000","jf":"0"},
{"jg":"20161111230000","jf":"0"},
{"jg":"20161112020000","jf":"0"},
{"jg":"20161112050000","jf":"0"},
{"jg":"20161112080000","jf":"0"},
{"jg":"20161112110000","jf":"0"},
{"jg":"20161112140000","jf":"0"},
{"jg":"20161112170000","jf":"0"}
]
},
"pm25":{
"key":"Xuchang",
"show_desc":0,
"pm25":{
"curPm":"50",
"pm25":"25",
"pm10":"51",
"level":1,
"quality":"优",
"des":"可正常活动。"
},
"dateTime":"2016年11月11日16时",
"cityName":"许昌"
},
"jingqu":"",
"jingqutq":"",
"date":"",
"isForeign":"0"
}
},
"error_code":0
})

这篇文章讲解的很详细:jsonp协议原理深度解析

(完)