$(document).ready(function(){
$("#btn").click(function(k) {
//...
var j = $("form").serializeArray();//序列化name/value
$.ajax({
type: 'GET', //这里用GET
url: 'ajax.php',
dataType: 'jsonp', //类型
data: j,
jsonp: 'callback', //jsonp回调参数,必需
async: false,
success: function(result) {//返回的json数据
alert(result.message); //回调输出
result = result || {};
if (result.msg=='err'){
alert(result.info);
}else if (result.msg=="ok"){
alert('提交成功');
}else{
alert('提交失败');
}
},
timeout: 3000
})
//...
});
});
<?php
$callback = isset($_GET['callback']) ? trim($_GET['callback']) : ''; //jsonp回调参数,必需
$date = array("age"=>$_GET['age'], "message"=>$_GET['age']);
$date["msg"]="err";
$date["info"]="因人品问题,发送失败";
$tmp= json_encode($date); //json 数据
echo $callback . '(' . $tmp .')'; //返回格式,必需
?>