zoukankan      html  css  js  c++  java
  • jsop之---实现过程

    JSONP(JSONP - JSON with Padding是JSON的一种“使用模式”),利用script标签的src属性(浏览器允许script标签跨域)

    跨域访问,非同源访问

    <!DOCTYPE html>
    <html>
    <head lang="en">
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>

        <p>
            <input type="button" onclick="Jsonp1();"  value='提交'/>
        </p>

        <p>
            <input type="button" onclick="Jsonp2();" value='提交'/>
        </p>
      底层真实的实现过程
        <script type="text/javascript" src="jquery-1.12.4.js"></script>
        <script>
            function Jsonp1(){
                var tag = document.createElement('script');
                tag.src = "http://c2.com:8000/test/";
                document.head.appendChild(tag);
                document.head.removeChild(tag);

            }
      封装后的使用方法
            function Jsonp2(){
                $.ajax({
                    url: "http://c2.com:8000/test/",
                    type: 'GET',
                    dataType: 'JSONP',
                    success: function(data, statusText, xmlHttpRequest){
                        console.log(data);

           jsonp: 'callback',     

          固定用法,加上以下两个参数,url相当于http://127.0.0.1:9000/xiaokai.html?callback=func
                     jsonpCallback: 'func'  指定对方需要返回的函数名是func
                })
            }
            function func(arg) {
                console.log(arg);
            }
        </script>
    </body>
    </html>

    基于JSONP实现跨域Ajax - Demo

  • 相关阅读:
    Jrain'Lのvueblog
    前端知识整理 の IMWeb
    js编程小练习1
    mac版本cornerstone的无限期破解方法(转)
    教你解锁被锁住的苹果mac电脑的文件跟文件夹,同时也可删除被锁的文件跟文件夹(转)
    Mac下配置svn服务器
    ios 查看模拟器路径以及应用的文件夹
    python怎么解压压缩的字符串数据
    python全局变量被覆盖的问题
    PyInstaller:把你的Python转为Exe
  • 原文地址:https://www.cnblogs.com/jinxf/p/9164360.html
Copyright © 2011-2022 走看看