spring mvc jsonp调用示例

服务端代码:主要是返回的时候,返回值要用callback包装一下

 /**
     * JSONP调用
     *
     * @param request
     * @return
     */
    @RequestMapping("/remote/jsonp")
    public void remoteJsonp(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String jsonpCallback = request.getParameter("jsonpCallback");
        String data=request.getParameter("data");
        //todo something
        
        ActionResultEntity result = new ActionResultEntity();
        //设置返回值
        String returnValue = jsonpCallback + "(" + StringUtil.toJsonString(result) + ")";
        response.getWriter().write(returnValue);
    }

js调用代码:

                            $.ajax({
                                async: false,
                                type: "post",
                                url:"http://localhost:8080/main/remote/jsonp",
                                data: {
                                    data: "test"
                                },
                                dataType: "jsonp",
                                jsonp: "jsonpCallback",
                                success: function (successJson) {
                                   
                                },
                                error: function (errorJson,text,message) {
                                   
                                }

 });

 

posted @ 2017-09-11 10:11  Gyoung  阅读(972)  评论(0编辑  收藏  举报