jsonp是啥玩意儿呢 ?你猜!

此感叹号意味着以下内容要说正事儿了!!!!!

由于浏览器为了安全限制(他要怎样他说了算,此处不扯远了):只能允许来自同服务器,同域名的同端口号下请求数据(同源),是的jsonp就是为了不受你限制而生的!

 

。》》。

老子实在不想写了 ,但是看别人写的太鸡巴啰嗦了,借别人代码说两句,就一句,你可能就懂了

 

 

<!DOCTYPE html>
<html>
<head>
    <title>GoJSONP</title>
</head>
<body>
<script type="text/javascript">
    function jsonhandle(data){
        alert("age:" + data.age + "name:" + data.name);
    }
</script>
<script type="text/javascript" src="jquery-1.8.3.min.js">
</script>
<script type="text/javascript">
    $(document).ready(function(){
        $.ajax({
            type : "get",
            async: false,
            url : "http://www.practice-zhao.com/student.php?id=1",
            type: "json",
            success : function(data) {
                jsonhandle(data);
            }

        });
    });
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
    <title>GoJSONP</title>
</head>
<body>
<script type="text/javascript">
    function jsonhandle(data){
        alert("age:" + data.age + "name:" + data.name);
    }
</script>
<script type="text/javascript" src="jquery-1.8.3.min.js">
</script>
<script type="text/javascript">
    $(document).ready(function(){
        var url = "http://www.practice-zhao.com/student.php?id=1&callback=jsonhandle";
        var obj = $('<script><\/script>');
        obj.attr("src",url);
        $("body").append(obj);
    });
</script>
</body>
</html>
http://www.practice-zhao.com/student.php?id=1&callback=jsonhandle 此地址是一个php的输出getInfo({"username":"张三"})

  代码估计你们也看得懂,重点!!重点来了!!!!!!所谓跨域,就是数据共享,接口请求做限制,但是没有对图片,文本,文件等在是否同一来源(同服务器,同域名的同端口号)做限制,比如cdn(此处有人白眼),所以在script标签的src其实是个get请求,这个请求可以是一个js文件也可以是一个js代码!总之它不是一个被限制跨域的接口请求!说白了就是去拿到js代码,想要传的数据整合在这串代码里了(呵呵,早期的web端推送就是这样的原理,push一段代码过来)

 

 

 

 

 

 

 

 

 

mmmp 终于写完了,我就是有点懒