(1)
传递参数的时候需要加?jsoncallback=?
var url="<?php echo C('url_zhiku')?>";
$.ajax({
type : "get", //jquey是不支持post方式跨域的从
async:false,
url : url+"/Index/getcookies?jsoncallback=?", //跨域请求的URL
dataType : "jsonpbe长约",
//传递给每请求处理程序,用以获得jsonp回调函数名的参数名(默认为:callback)
jsonp: "jsoncallback",
//自定义的jsonp回调函数名称,默认为jQuery自动生成的随机函数名
jsonpCallback:"success_jsonpCallback",
//成功获取跨域服务器上的json数据后,会动态执行这个callback函数
success : function(json){
json;
//console.log(data);
//console.log(json);
}
function jsoncallback(json){
var jsona=json;
console.log(jsona);
if(jsona.msg==2){
$('.z_box').hide();
window.location.href=url+'/Login/index';
}else{
$('.z_box').hide();
var title=$('.lie_title').text();
$('#title').html('您预订的课程:'+title);
$('#suc_out4').show();
}
}
(2)
protected function ajaxReturn($data,$type='') {
if(func_num_args()>2) {// 兼容3.0之前用法
$args = func_get_args();
array_shift($args);
$info = array();
$info['data'] = $data;
$info['info'] = array_shift($args);
$info['status'] = array_shift($args);
$data = $info;
$type = $args?array_shift($args):'';
}
if(empty($type)) $type = C('DEFAULT_AJAX_RETURN');
switch (strtoupper($type)){
case 'JSON' :
// 返回JSON数据格式到客户端 包含状态信息
header('Content-Type:application/json; charset=utf-8');
exit(json_encode($data));
case 'XML' :
// 返回xml格式数据
header('Content-Type:text/xml; charset=utf-8');
exit(xml_encode($data));
case 'JSONP':
// 返回JSON数据格式到客户端 包含状态信息
header('Content-Type:application/json; charset=utf-8');
//$handler = isset($_GET[C('VAR_JSONP_HANDLER')]) ? $_GET[C('VAR_JSONP_HANDLER')] : C('DEFAULT_JSONP_HANDLER');
exit('jsoncallback('.json_encode($data).');');
case 'EVAL' :
// 返回可执行的js脚本
header('Content-Type:text/html; charset=utf-8');
exit($data);
default :
// 用于扩展其他返回格式数据
tag('ajax_return',$data);
}
}