We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
首先JSON是一种基于文本的数据交换方式,或者叫做数据描述格式 当一个网页在请求JavaScript文件时则不受是否跨域的影响,凡是拥有”src”这个属性的标签都拥有跨域的能力,比如<script>、<img>、<iframe> 所以我们这里运用了script标签的跨域能力,让它用一个callback函数包裹着一段JSON格式的数据,当该数据返回到前端页面的时候,我们再执行这个函数就可以把数据读取出来 前端代码 jsonp.html
<script>、<img>、<iframe>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Wsscat's jsonp</title> </head> <body> <button onclick="jsonpServer('jsonp.php')">JSONP</button> </body> <script> function jsonpServer(url) { var script = document.createElement("script"); script.setAttribute("type", "text/javascript"); script.setAttribute("src", url); document.body.appendChild(script); } function JSON_CALLBACK(data) { console.log(data); } </script> </html>
后端代码 jsonp.php
<?php $data = '[{"id":"1","name":"wsscat"},{"id":"2","name":"asw"}]'; $data = "JSON_CALLBACK(" . $data . ")"; echo $data; ?>
The text was updated successfully, but these errors were encountered:
谢谢
Sorry, something went wrong.
js代码
$.ajax({ url: 'index.php', type: 'get', dataType: 'jsonp', //jsonp:'JSON_CALLBACK', jsonpCallback: 'JSON_CALLBACK', success: function (data) { console.log(data) } })
php代码
thank you!
No branches or pull requests
首先JSON是一种基于文本的数据交换方式,或者叫做数据描述格式
当一个网页在请求JavaScript文件时则不受是否跨域的影响,凡是拥有”src”这个属性的标签都拥有跨域的能力,比如
<script>、<img>、<iframe>
所以我们这里运用了script标签的跨域能力,让它用一个callback函数包裹着一段JSON格式的数据,当该数据返回到前端页面的时候,我们再执行这个函数就可以把数据读取出来
前端代码
jsonp.html
后端代码
jsonp.php
The text was updated successfully, but these errors were encountered: