服务端代码:主要是返回的时候,返回值要用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) {
                                   
                                }

 });

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-05-15
  • 2022-12-23
  • 2022-12-23
  • 2021-06-11
  • 2022-02-27
  • 2021-07-06
猜你喜欢
  • 2021-11-20
  • 2021-06-09
  • 2022-12-23
  • 2021-11-07
  • 2021-12-03
  • 2021-10-08
  • 2021-11-28
相关资源
相似解决方案