-
Notifications
You must be signed in to change notification settings - Fork 735
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Javascript的jsonp原理 #55
Labels
Comments
谢谢 |
jQuery的jsonp方法
js代码 $.ajax({
url: 'index.php',
type: 'get',
dataType: 'jsonp',
//jsonp:'JSON_CALLBACK',
jsonpCallback: 'JSON_CALLBACK',
success: function (data) {
console.log(data)
}
}) php代码 <?php
$data = '[{"id":"1","name":"wsscat"},{"id":"2","name":"asw"}]';
$data = "JSON_CALLBACK(" . $data . ")";
echo $data;
?> |
thank you! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
首先JSON是一种基于文本的数据交换方式,或者叫做数据描述格式
当一个网页在请求JavaScript文件时则不受是否跨域的影响,凡是拥有”src”这个属性的标签都拥有跨域的能力,比如
<script>、<img>、<iframe>
所以我们这里运用了script标签的跨域能力,让它用一个callback函数包裹着一段JSON格式的数据,当该数据返回到前端页面的时候,我们再执行这个函数就可以把数据读取出来
前端代码
jsonp.html
后端代码
jsonp.php
The text was updated successfully, but these errors were encountered: