Jsonp接口

JSONP的接口
<?php
/**
 * Created by PhpStorm.
 * User: machenike
* Date: 2016/4/28
* Time: 16:04
*/
$name=isset($_GET['ad_name'])?$_GET['ad_name']:"北京";
//echo $name;die;
header('content-type:text/html;charset=utf-8');
$pdo=new PDO('mysql:host=127.0.0.1;dbname=ecshop','root','root',array(PDO::MYSQL_ATTR_INIT_COMMAND=>'set names utf8'));
$arr=$pdo->query("select * from address where ad_name='$name'");
$arr->setFetchMode(PDO::FETCH_ASSOC);
$ar=$arr->fetchAll();
$data=json_encode($ar);
$jsoncallback = isset($_REQUEST ['jsoncallback'])?htmlspecialchars($_REQUEST ['jsoncallback']):"data";
echo $jsoncallback ."(" . $data . ")";



JSONP接口的调用
<script src="../jquery-2.1.4.min.js"></script>
<script>
    var name='<?php echo urlencode("北京")?>';
    var url="http://www.cishop.com/jiekou/jiekou_jsonp1.php?ad_name="+name+"&format=json&jsoncallback=?";
    var ar='';
    $.getJSON(url,function(msg){
        console.log(msg);
        var data = eval(msg)
        var str ="";
        for(var i=0;i<data.length;i++){
            str+="<p>"+data[i]["ad_name"]+"</p>"
        }
        $('#ss').html(str);
    })
</script>
<div id='ss'></div>


你可能感兴趣的:(Jsonp接口)