怀化新站优化,广州网站外贸推广,做网站需要会的软件,重庆网站建设seoJSONP 教程 本章节我们将向大家介绍 JSONP 的知识。 Jsonp(JSON with Padding) 是 json 的一种使用模式#xff0c;可以让网页从别的域名#xff08;网站#xff09;那获取资料#xff0c;即跨域读取数据。 为什么我们从不同的域#xff08;网站#xff09;访… JSONP 教程 本章节我们将向大家介绍 JSONP 的知识。 Jsonp(JSON with Padding) 是 json 的一种使用模式可以让网页从别的域名网站那获取资料即跨域读取数据。 为什么我们从不同的域网站访问数据需要一个特殊的技术(JSONP )呢这是因为同源策略。 同源策略它是由Netscape提出的一个著名的安全策略现在所有支持JavaScript 的浏览器都会使用这个策略。 JSONP 应用 1. 服务端JSONP格式数据 如客户想访问 : http://www.rm5u.com/try/ajax/jsonp.php?jsonpcallbackFunction。 假设客户期望返回JSON数据[customername1,customername2]。 真正返回到客户端的数据显示为: callbackFunction([customername1,customername2])。 服务端文件jsonp.php代码为 ?php
header(Content-type: application/json);
//获取回调函数名
$jsoncallback htmlspecialchars($_REQUEST [jsoncallback]);
//json数据
$json_data [customername1,customername2];
//输出jsonp格式的数据
echo $jsoncallback . ( . $json_data . );
?
2. 客户端实现 callbackFunction 函数 script typetext/javascript
function callbackFunction(result, methodName)
{
var html ul;
for(var i 0; i result.length; i )
{
html li result[i] /li;
}
html /ul;
document.getElementById(divCustomers).innerHTML html;
}
/script
页面展示 div iddivCustomers/div
客户端页面完整代码 !DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
html xmlnshttp://www.w3.org/1999/xhtml
head
titleJSONP 实例/title
/head
body
div iddivCustomers/div
script typetext/javascript
function callbackFunction(result, methodName)
{
var html ul;
for(var i 0; i result.length; i )
{
html li result[i] /li;
}
html /ul;
document.getElementById(divCustomers).innerHTML html;
}
/script
script typetext/javascript srchttp://www.rm5u.com/try/ajax/jsonp.php?jsoncallbackcallbackFunction/script
/body
/html
jQuery 使用 JSONP 以上代码可以使用 jQuery 代码实例 !DOCTYPE html
html
head
titleJSONP 实例/title
script srchttp://apps.bdimg.com/libs/jquery/1.8.3/jquery.js/script
/head
body
div iddivCustomers/div
script
$.getJSON(http://www.rm5u.com/try/ajax/jsonp.php?jsoncallback?, function(data) {
var html ul;
for(var i 0; i data.length; i )
{
html li data[i] /li;
}
html /ul;
$(#divCustomers).html(html);
});
/script
/body
/html
更多专业前端知识请上
【猿2048】www.mk2048.com