jsonp例子php

html:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>test</title>
<script src="./jquery.min.js"></script>
<script src="./ajax.js"></script>
</head>

<body>
<form name="form">
<input type="text" name="names">
<input type="text" name="ages">
<input type="button" id="btn" value="button" />
</form>
</body>
</html>

 

jquery文件自己准备

ajax.js:

$(document).ready(function(){

$("#btn").click(function(k) {
//...
var j = $("form").serializeArray();//序列化name/value
$.ajax({
type: 'GET', //这里用GET
url: 'http://woaiwojia.online/jsonp/index.php',
dataType: 'jsonp', //类型
data: j,
jsonp: 'callback', //jsonp回调参数,必需
jsonpCallback:"success_jsonpCallback",
async: false,
success: function(result) {//返回的json数据
alert(result.names+'**'+result.ages); //回调输出



},

})
//...
});

});

 

index.php

<?php
$callback = isset($_GET['callback']) ? trim($_GET['callback']) : ''; //jsonp回调参数,必需
$date = array("names"=>$_GET['names'], "ages"=>$_GET['ages']);
$tmp= json_encode($date); //json 数据
echo $callback . '(' . $tmp .')'; //返回格式,必需
?>

posted on 2017-12-28 16:56  luziluck  阅读(133)  评论(0编辑  收藏  举报

导航