zoukankan      html  css  js  c++  java
  • springmvc jsonp 跨域调用的例子

    html代码 :

    <!DOCTYPE html>
    <html>
    <head>
        <title>json跨域调用的例子</title>
    </head>
    <body>
    <script type="text/javascript" src="http://libs.baidu.com/jquery/2.1.1/jquery.min.js">
    </script>
    <script type="text/javascript">
        $(document).ready(function(){
            $.ajax({
                type : "get",
                async: false,
                url: "http://127.0.0.1:8080/seg/getSeg?chineseSentence=%E6%88%91%E7%88%B1%E5%8C%97%E4%BA%AC"+'&callback=?',//请求的地址
                dataType: "jsonp",
                jsonp:"callback", //这个参数会出现在springmvc的方法中
                success : function(data) {
                    
                    alert(data.Age+' '+data.UserName);
                    
                }

            });
        });
    </script>
    </body>
    </html>

    springmvc控制器java代码:

    package com.example.segment.controller;

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.ResponseBody;

    @Controller
    @RequestMapping(value = "/seg/*")
    public class ChineseWordsSegmentation {

        private static final Logger LOG = LoggerFactory.getLogger(ChineseWordsSegmentation.class);
        private ChineseWordsSegment seg;

        @RequestMapping(value = "getSeg", method = RequestMethod.GET, produces = {"application/json; charset=UTF-8"})
        @ResponseBody
        public String getSeg(@RequestParam String chineseSentence, String callback){
            String jsonp = "";
            String result = "{"UserName":"abc","Age":"25"}";

            LOG.info(seg.getSegmentation(chineseSentence).toString());


            if(callback.length() > 0){

                jsonp = callback + "(" + result + ")";

            }
            return jsonp;


        }

     

    }

  • 相关阅读:
    设计模式学习总结系列应用实例
    【研究课题】高校特殊学生的发现及培养机制研究
    Linux下Oracle11G RAC报错:在安装oracle软件时报file not found一例
    python pro practice
    openstack python sdk list tenants get token get servers
    openstack api
    python
    git for windows
    openstack api users list get token get servers
    linux 流量监控
  • 原文地址:https://www.cnblogs.com/herosoft/p/8288938.html
Copyright © 2011-2022 走看看