zoukankan      html  css  js  c++  java
  • JSONP

    读这篇文章需要5分钟

    个人总结:

    jsonp:

    ajax不能跨域但是js是可以跨域的,

    所以如果.html的某个本地script标签中有个函数,

    而另一个来自远程的script src='remote.js'里面写的是运行这个函数,并传入参数。

    那么这个函数就可以被运行了。

    将src属性变成请求而不是写死,就可以动态处理函数的调用。

    ajax是不能跨域的:

    但是script标签可以跨域

    remote.js:

    alert('我是远程文件');

    1、我们知道,哪怕跨域js文件中的代码(当然指符合web脚本安全策略的),web页面也是可以无条件执行的::

    local:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title></title>
    <script type="text/javascript" src="http://remoteserver.com/remote.js"></script>
    </head>
    <body>
    </body>
    </html>

    2、现在我们在jsonp.html页面定义一个函数,然后在远程remote.js中传入数据进行调用:

    local:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title></title>

    <script type="text/javascript">

    var localHandler = function(data){
    alert('我是本地函数,可以被跨域的remote.js文件调用,远程js带来的数据是:' + data.result);
    };

    </script>

    <script type="text/javascript" src="http://remoteserver.com/remote.js"></script>
    </head>
    <body>

    </body>
    </html>

    remote.js:

    localHandler({"result":"我是远程js带来的数据"});

    3、聪明的开发者很容易想到,只要服务端提供的js脚本是动态生成的就行了呗,这样调用者可以传一个参数过去告诉服务端 “我想要一段调用XXX函数的js代码,请你返回给我”,于是服务器就可以按照客户端的需求来生成js脚本并响应了:

    local:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title></title>
    <script type="text/javascript">

    var flightHandler = function(data){
    alert('你查询的航班结果是:票价 ' + data.price + ' 元,' + '余票 ' + data.tickets + ' 张。');
    };

    var url = "http://flightQuery.com/jsonp/flightResult.aspx?code=CA1998&callback=flightHandler";

    var script = document.createElement('script');


    script.setAttribute('src', url);

    document.getElementsByTagName('head')[0].appendChild(script);


    </script>
    </head>
    <body>
    </body>

    </html>

    这次的代码变化比较大,不再直接把远程js文件写死,而是编码实现动态查询,而这也正是jsonp客户端实现的核心部分,本例中的重点也就在于如何完成jsonp调用的全过程。 
    我们看到调用的url中传递了一个code参数,告诉服务器我要查的是CA1998次航班的信息,而callback参数则告诉服务器,我的本地回调函数叫做flightHandler,所以请把查询结果传入这个函数中进行调用。 
    OK,服务器很聪明,这个叫做flightResult.aspx的页面生成了一段这样的代码提供给jsonp.html 

    remote.js:

    flightHandler({
    "code": "CA1998",
    "price": 1780,
    "tickets": 5
    });

    4、到这里为止的话,相信你已经能够理解jsonp的客户端实现原理了吧?剩下的就是如何把代码封装一下,以便于与用户界面交互,从而实现多次和重复调用:

    JSONP stands for JSON with Padding. Requesting a file from another domain can cause problems, due to cross-domain policy. Requesting an external script from another domain does not have this problem. JSONP uses this advantage, and request files using the script tag instead of the XMLHttpRequest object.

    https://en.wikipedia.org/wiki/JSONP

  • 相关阅读:
    c语言求最大公约数和最小公倍数(转)
    git 提交去除每次输账号密码
    phpstorm使用zen coding 快速编辑补全html/css代码
    YII2.0使用ActiveForm表单(转)
    php面向对象之trait
    php操作redis(转)
    模块
    列表生成式 与生成器表达式
    三元表达式,递归,内置函数
    面向过程的编程思想
  • 原文地址:https://www.cnblogs.com/eret9616/p/9242021.html
Copyright © 2011-2022 走看看