Use simple records with jsonp (1)
Jul 29, 2016 am 09:06 AM
gt
jsonp
lt
quot
script
The usage environment of jsonp is generally available for front-end and back-end exchange when logging in or exchanging data in different domains
Principle:
The introduced js can be from different domains, and the js file can be generated from the background
(What is said here is a bit simple, please go there more Find information)
Principle:
The introduced js can be from different domains, and the js file can be generated from the background
(What is said here is a bit simple, please go there more Find information)
Use DEMO:
html: //原生js <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script type="text/javascript"> // 得到航班信息查询结果后的回调函数 var returnjs = function(data){ alert(data.code); }; // 提供jsonp服务的url地址(不管是什么类型的地址,最终生成的返回值都是一段javascript代码) var url = "http://www.return.com/jsonp/get?code=1&callback=returnjs";//数据接收后台 // 创建script标签,设置其属性 var script = document.createElement('script'); script.setAttribute('src', url); // 把script标签加入head,此时调用开始 document.getElementsByTagName('head')[0].appendChild(script); </script> </head> <body> </body> </html>
Copy after login
//jQ版 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here1</title> <script type="text/javascript" src="jq.js"></script><!-- 记得引入jq --> </head> <body> <script type="text/javascript"> jQuery(document).ready(function(){ $.ajax({ type: "get", async: false, url: "http://www.return.com/jsonp/get?code=1",//数据接收后台 dataType: "jsonp", jsonp: "callback",//传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名(一般默认为:callback) jsonpCallback:"returnjs",//自定义的jsonp回调函数名称,默认为jQuery自动生成的随机函数名,也可以写"?",jQuery会自动为你处理数据 crossDomain:true, success: function(json){ alert(json.code); }, error: function(){ alert('fail'); } }); }); </script> </body> </html>
Copy after login
后台PHP: <?php class jsonp{ public function get(){ $code=$_GET['code']; if($code==1){ $code=2; } echo 'returnjs({"code":"'.$code.'"})'; } }
Copy after login
The above introduces the simple record of using jsonp (1), including the content. I hope it will be helpful to friends who are interested in PHP tutorials.
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
R.E.P.O. Save File Location: Where Is It & How to Protect It?
3 weeks ago
By DDD

Hot tools Tags

Hot Article
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
R.E.P.O. Save File Location: Where Is It & How to Protect It?
3 weeks ago
By DDD

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

What are the differences between Huawei GT3 Pro and GT4?

Fix: Snipping tool not working in Windows 11

How to Fix Can't Connect to App Store Error on iPhone

How to use JSONP to implement cross-domain requests in Vue
