前端代码
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<script typet="text/javascript" src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>
<body>
<div id="divCustomers"></div>
</body>
<script type="text/javascript">
var url='http://xxx/index.php?g=M&m=Index&a=getjsonpData';
$.ajax({
url:url,
dataType:'jsonp',
jsonp:'jsoncallback', //Jquery生成验证参数的名称
processData: false,
type:'get',
success:function(data){
var html = '<ul>';
for(var i = 0; i < data.length; i++)
{
html += '<li>' + data[i] + '</li>';
}
html += '</ul>';
$('#divCustomers').html(html);
},
});
</script>
</html>
public function getjsonpData(){
header('Content-type: application/json');
header('Access-Control-Allow-Origin: *');
$json_data = '["customername1","customername2"]';
//$_GET['jsoncallback'] 对应 jsonp:'jsoncallback',
echo $_GET['jsoncallback'] . "(" . $json_data . ")";die;
}