记录下--java返回jsonp案例梳理

*jsonp 主要是利用

<script src=“http://IP:端口号/></script>

			来进行跨域;可以解决兼容性的问题
			利用<script>标签的跨域功能,同时要求后台返回的数据应该是以方法调用形式进行返回

<script src=“http://localhost:端口/jquery.js”>
			回调函数(data);
		</script>*

执行的成功在于这里
response.getWriter().write(“alert(123)”);//返回信息
response.getWriter().flush();
response.getWriter().close();
后台返回json数据

  @RequestMapping("/login1")
    @ResponseBody
    public String  ll(HttpServletResponse response,HttpServletRequest request) throws IOException {
        Subject subject = SecurityUtils.getSubject();
        UUser principal = (UUser)subject.getPrincipal();
        String s= JSONObject.toJSONString(principal);//获取用户信息
        response.getWriter().write("var uuser="+s);//返回信息 
        response.getWriter().flush();
        response.getWriter().close();
        return  null;
    }

前端用 script标签接受演示如下

<!-- 接口信息 http://localhost:8081/test/login1-->
<script src="http://localhost:8081/test/login1"></script>
<!-- 打印jsonp 中的 uuser 数据-->
<script> console.log(uuser)</script>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
hello word
</body>
</html>

图片详情 可以看出 信息已取回 输入uuser 返回 后台的用户信息

在这里插入图片描述
返回数据

var uuser{"createTime":940348800000,"email":"email","id":1,"lastLoginTime":940438800000,"nickname":"admin","pswd":"4297f44b13955235245b2497399d7a93","status":1}

输入 uuser.nickname 返回用户名 admin

在这里插入图片描述
后台 返回jsonp 返回 来执行 js函数

  @RequestMapping("/login2")
    @ResponseBody
    public String  lll(HttpServletResponse response,HttpServletRequest request) throws IOException {
        response.getWriter().write("alert(123)"); //返回浏览器执行 js函数
        response.getWriter().flush();
        response.getWriter().close();
        return  null;
    }

前台加上 script 标签

<script src="http://localhost:8081/test/login2"></script>

执行成功
js函数
案例返回 js函数

 @RequestMapping("/login3")
    @ResponseBody
    public String  login3(HttpServletResponse response,HttpServletRequest request) throws IOException {
        response.setCharacterEncoding("utf-8"); 
        //执行下js jsonp1里的 doucment.write() 函数  jsonp2 再次执行 jsonp1函数
        response.getWriter().write("function jsonp1() {\n" +
                "            document.write(\"我是JSONP\");\n" +
                "        };"+
                " function jsonp2() {\n" +
                "            jsonp1();" +
                "        }");
        response.getWriter().flush();
        response.getWriter().close();
        return  null;
    }

前端代码

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
hello word
<script src="http://localhost:8081/test/login3">
 //jsonp请求发送
</script>
</body>
</html>

jsonp1() 执行 浏览器 打印出 “我是jsonp”
在这里插入图片描述
jsonp2() 函数执行 调动 jsonp1函数 调动 打印成功
在这里插入图片描述
如有错误请指示

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JSONObject必包的Jar包及json生成的简单案例 所有commons包的网址: http://commons.apache.org/index.html 组装和解析JSONObject的Json字符串,共需要下面六个包: 1、json-lib 2、commons-beanutils 3、commons-collections 4、commons-lang 5、commons-logging 6、ezmorph 第零个包: json-lib-2.4-jdk15.jar http://sourceforge.net/projects/json-lib/files/json-lib/json-lib-2.4/ 下载地址:http://nchc.dl.sourceforge.net/project/json-lib/json-lib/json-lib-2.4/json-lib-2.4-jdk15.jar 第一个包: commons-beanutils-1.9.2.jar http://commons.apache.org/proper/commons-beanutils/download_beanutils.cgi 下载地址:http://mirrors.cnnic.cn/apache//commons/beanutils/binaries/commons-beanutils-1.9.2-bin.zip 第二个包: (注:此包不可用,改用旧包) commons-collections4-4.0.jar http://commons.apache.org/proper/commons-collections/download_collections.cgi 下载地址:http://apache.dataguru.cn//commons/collections/binaries/commons-collections4-4.0-bin.zip (注:此包可用,低版本的包稳定性更高) commons-collections-3.2.1.jar http://commons.apache.org/proper/commons-collections/download_collections.cgi 下载地址:http://mirrors.hust.edu.cn/apache//commons/collections/binaries/commons-collections-3.2.1-bin.zip 第三个包: (注:此包不可用,会造成程序出错,改用旧包) commons-lang3-3.3.2.jar http://commons.apache.org/proper/commons-lang/download_lang.cgi 下载地址:http://apache.dataguru.cn//commons/lang/binaries/commons-lang3-3.3.2-bin.zip (注:此包可用,低版本的包稳定性更高) commons-lang-2.6-bin http://commons.apache.org/proper/commons-lang/download_lang.cgi?Preferred=http%3A%2F%2Fapache.dataguru.cn%2F 下载地址:http://apache.dataguru.cn//commons/lang/binaries/commons-lang-2.6-bin.zip 第四个包: commons-logging-1.1.3.jar http://commons.apache.org/proper/commons-logging/download_logging.cgi 下载地址:http://apache.dataguru.cn//commons/logging/binaries/commons-logging-1.1.3-bin.zip 第五个包: ezmorph-1.0.2.jar http://ezmorph.sourceforge.net/ http://sourceforge.net/projects/ezmorph/files/ezmorph/ 下载地址:http://nchc.dl.sourceforge.net/project/ezmorph/ezmorph/ezmorph-1.0.6/ezmorph-1.0.6.jar
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值