Home Backend Development PHP Tutorial Use simple records with jsonp (1)

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)

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 尊渡假赌尊渡假赌尊渡假赌

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 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

What are the differences between Huawei GT3 Pro and GT4?

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Fix: Snipping tool not working in Windows 11

How to Fix Can't Connect to App Store Error on iPhone How to Fix Can't Connect to App Store Error on iPhone Jul 29, 2023 am 08:22 AM

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

What does script mean? What does script mean? Aug 29, 2023 pm 02:00 PM

What does script mean?

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决

How to use JSONP to implement cross-domain requests in Vue How to use JSONP to implement cross-domain requests in Vue Oct 15, 2023 pm 03:52 PM

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

what is script what is script Oct 12, 2023 am 10:04 AM

what is script

How to solve scripterror How to solve scripterror Oct 18, 2023 am 09:44 AM

How to solve scripterror

See all articles