美文网首页
跨域请求-JSONP

跨域请求-JSONP

作者: 毛不翼 | 来源:发表于2019-10-12 11:13 被阅读0次

前端

(function ($) {
    $.loginCheck = function (pram1, pram2, success, error) {
        let URL_DOMAIN = "//xxx.fireface.cn/xxx/xx/xx?";
        let url = URL_DOMAIN + "pram1=" + pram1+ "&pram2=" + pram2;
        call(url,success,error)

    };
    $.getAuth = function (success, error) {
        let AUTH_URL = "//xxx.fireface.cn/xxx/xx/xx?";
        call(AUTH_URL,success, error);
    };
    let call = function (url, success, error) {
        url += url.indexOf("?") > 1 ? "&callback=callback" : "?callback=callback";
        let brake = false;
        let head = document.getElementsByTagName('head')[0];
        let script = document.createElement('script');
        head.appendChild(script);

        window.callback = function (data) {
            if (brake) {
                return false;
            }
            brake = true;
            head.removeChild(script);
            clearTimeout(script.timer);
            success && success(data);
        };

        script.src = url;
        script.timer = setTimeout(function () {
            if (brake) {
                return false;
            }
            brake = true;
            head.removeChild(script);
            error && error({
                message: '超时'
            });
        }, 10000);
    };
})(LoginCaller = {});

function successCallback(responseText){
    //兼容转换为对象,浏览器差异,可是是json串或者是object类型
    var data= typeof responseText == 'string' ? JSON.parse(responseText) : responseText;
    console.log(data.success);
    console.log(data.code);
}
function errorCallback(responseText){
    //兼容转换为对象,浏览器差异,可是是json串或者是object类型
    var data = typeof responseText == 'string' ? JSON.parse(responseText) : responseText;
    console.log(data.message);
    console.log(data.code);
}
 
LoginCaller.getAuth(successCallback,errorCallback);

后端

 public String XXX() {
        try {
            String pram1= request.getParameter("pram1");
            String pram2= request.getParameter("pram2");
            String callback = request.getParameter("callback");

            Map<String, Object> resultMap = new HashMap<>();
            if (pram1 != null) {
                if (pram1.equalsIgnoreCase(pram1)) {
                    setCookie(response, "pram1", pram1);
                    resultMap.put(SUCCESS, "true");
                } else {
                    resultMap.put(SUCCESS, "false");
                }

            }
            PrintWriter writer = response.getWriter();
            writer.append(callback).append("(").append(JSON.toJSONString(resultMap)).append(")");
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

相关文章

  • ajax跨域请求

    ajax跨域请求(jsonp) 利用JSONP解决AJAX跨域问题的原理与jQuery解决方案JSONP jQue...

  • 做demo和学习过程当中遇到的一些问题,收集的博文

    轻松搞定JSONP跨域请求--->关键字: 跨域, 同源策略, JSONP原理 git拉取远程分支到本地 git ...

  • 跨域

    ??JSONP只能解决GET请求跨域,不能解决POST请求跨域问题,XHR2可以解决GET,POST方式的请求跨域...

  • JavaScript - GET/POST及跨域方法

    xhr 原生方法请求 window fetch 方法 关于跨域 利用JSONP实现跨域调用 使用 CORS(跨域资...

  • javasscript - 收藏集 - 掘金

    jsonp 跨域请求详解——从繁至简 - 前端 - 掘金什么是jsonp?为什么要用jsonp?JSONP(JSO...

  • JSONP技术原理及实现

    转自《跨域jsonp的原理》 首先确定为什么要用jsonp,因为要跨域请求数据,那为什么会发生跨域呢,因为浏览器的...

  • js跨域问题

    来源 javascript中实现跨域的方式总结 第一种方式:jsonp请求;jsonp的原理是利用 标签的跨域特性...

  • 跨域实战解决方案

    一.跨域方案 1.JSONP跨域 (1)前端发起jQuery ajax 的get请求 $.getJSON...

  • Ajax下

    一、cors跨域请求 二、jsonp百度搜索的例子 jsonp.html jsonp.js

  • JSONP的劫持

    关于 JSONP JSONP 全称是 JSON with Padding ,是基于 JSON 格式的为解决跨域请求...

网友评论

      本文标题:跨域请求-JSONP

      本文链接:https://www.haomeiwen.com/subject/odqqmctx.html