Jquery跨域调用struts2的Action返回json数据易出现的问题

必须有jsoncallback


1.
$.getJSON("/queryName.action?jsoncallback=?",function(json){
                 alert(" json ="+ json );
                 console.log( json );
            });



2.

            < action name = "queryName" method = "queryName" class = "testAction" >
                 < result name = "success" type = "json" >
                      < param name="callbackParameter">jsoncallbackparam>
                 result >  
            action >


3.

try {
                 userList = userService .queryInfoByUserId( userId );
                 if (!CollectionUtils.isEmpty( userPostList )){
                     code = userList .get(0).getCode();
                     name = userList .get(0).getName();
                }
              
                Map map = new HashMap();
                map.put( "name" , name);
                map.put( "id" , code);
                JSONArray arr=JSONArray .fromObject(map);
                String  callback = jsoncallback;
             String jsoncallback = callback+ "({'result':" +arr+ "})" ;
             JsonFormatUtil.returnJsonResponse(jsoncallback);
           } catch (Exception e) {
                JsonFormatUtil.returnErrorMsgInfoResponse(e.getMessage());
                e.printStackTrace();
           }
           
            return null ;

你可能感兴趣的:(Java)