模板网站和定制网站的区别是什么,东莞 网站建设企业,大朗做网站公司,郴州新网交友信息1 概述
定义 json存在的意义#xff1a; 不同类型的语言#xff0c;都能识别json JSONP(JSON with Padding)是JSON的一种“使用模式”#xff0c;可用于解决主流浏览器的跨域数据访问的问题。由于同源策略#xff0c;一般来说位于 server1.example.com 的网页无法与不是 s…1 概述
定义 json存在的意义 不同类型的语言都能识别json JSONP(JSON with Padding)是JSON的一种“使用模式”可用于解决主流浏览器的跨域数据访问的问题。由于同源策略一般来说位于 server1.example.com 的网页无法与不是 server1.example.com的服务器沟通而 HTML 的 跨域浏览器A从服务器B获取的静态资源包括Html、Css、Js然后在Js中通过Ajax访问C服务器的静态资源或请求。即浏览器A从B服务器拿的资源资源中想访问服务器C的资源。同源策略同一个请求协议(如Http或Https)、同一个Ip、同一个端口3个全部相同即为同源。
2 demo
后端
?php$arr[namewoniu,age20];//把数组转成json字符串,$json json_encode($arr);//输出echo $json;
?前端
!DOCTYPE html
html langen
headmeta charsetUTF-8meta nameviewport contentwidthdevice-width, initial-scale1.0titleDocument/titlescript src./jquery-3.4.1.min.js/script
/head
bodyh1前端/h1script$.getJSON(后端的地址,function(data){alert(JSON.stringify(data));})/script
/body
/html
用户访问
192.168.190.134/woniu/demo31.html查看控制台 解决方案
JSONPCORS
3 JSONP 后端
?php$arr[namewoniu,age20];//把数组转成json字符串,$json json_encode($arr);$callback $_GET[callback]; // 函数对象, 字符串//输出///echo $json;echo $callback($json);
?前端
# 方法一
!DOCTYPE html
html langen
headmeta charsetUTF-8meta nameviewport contentwidthdevice-width, initial-scale1.0titleDocument/titlescript src./jquery-3.7.1.min.js/script
/head
bodyh1前端/h1script// function xxx(data){// // 后端返回的data json// console.log(999);// }/scriptscript$.getJSON(http://192.168.190.133/wh069/demo31.php?callback?,function(data){alert(JSON.stringify(data));// alert(JSON.parse(data)); // 反序列化 // json序列化和反序列化})/script
/body
/html# 方法二
!DOCTYPE html
html langen
headmeta charsetUTF-8meta nameviewport contentwidthdevice-width, initial-scale1.0titleDocument/titlescript src./jquery-3.7.1.min.js/script
/head
bodyh1前端/h1scriptfunction xxx(data){// 后端返回的data jsonalert(JSON.stringify(data));}/scriptscript srchttp://192.168.190.133/wh069/demo31.php?callbackxxx/script
/body
/html