您的位置:首页 > Web前端 > JavaScript

解决ajax跨域问题

2015-12-02 15:26 405 查看
前台:

$.ajax({

              type:'get',

              url:urly+'url?iType=zxxx&callback=?',

              contentType: "application/json; charset=utf-8",

              dataType: "jsonp", 

              processData: false

              success:function(data){

                   for(var i=0; i<data.content.length; i++){

                      var tab = "<tr><td></td><td><a  href='"+urly+"/gmcloud/interf/getZx.haze?
iId="+data.content[i].iId+"' target='_Blank' style='text-
decoration: none;'>"+data.content[i].iTli.substring(0,19)+"</a></td>
</tr>";

                      $("#xx").append(tab);

                  }

              },     

              error: function(err) {

//                       alert("获取失败。");

              }
             });

后台(注意最后需要用callback在最外层包一下):

@RequestMapping("getByMh")

    public void getByMh( HttpServletResponse response,HttpServletRequest request,InformationBean information) throws Exception{

        response.setContentType("text/html;charset=UTF-8");

        String msg = "";

        int code = 0;

        Map map = new HashMap();

        String callback = request.getParameter("callback");

            information.setiType(request.getParameter("iType"));

            List<InformationBean> list = informationService.getByMh(information);

            if(0!=list.size()&&!"".equals(list)){

                map.put("content", list);

                code=0;

                msg="获取信息成功!";

            }else{

                map.put("content", "");

                code=40029;

                msg="无";

            }try{

        }catch(Exception e){

            code=40029;

            msg="获取信息失败";

        }

            map.put("code", code);

            map.put("msg", msg); 

             PrintWriter out = response.getWriter();

            JSONObject jsonObject = JSONObject.fromObject(map);

             out.print(callback + "(" + jsonObject + ")");  
    }  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息