jquery中ajax请求全过程

本人比较容易这种格式性的东西,所以记一笔

$(".edit-store").click(function(){
         $.ajax({
            url: "get_store_info",//1.请求的action,点击按钮后先执行该请求,到服务器后台
            type: "post",
            dateType: "json",
            success: function(json){ //3.当执行后返回json类型的数据时,执行
                var data = $.parseJSON(json);
                var aboutUs = data["aboutUs"];
                var publicNotice = data["publicNotice"];
                var picPath = data["picPath"];
                var picPaths = new Array();
                var picPaths = picPath.split(",");

                $(".aboutUs").val(aboutUs);
                $(".publicNotice").val(publicNotice);
                $("#img1").attr("src","../uploadimage/" + picPaths[0]);
                $("#img2").attr("src","../uploadimage/" + picPaths[1]);
                $("#img3").attr("src","../uploadimage/" + picPaths[2]);

            }
        });

后台接受该请求

 <action name="get_store_info" class="com.admin.StoreList" method="getStoreInfo">
 </action>
public String getStoreInfo(){ //2.接受请求执行,并返回数据
        singleStore = StoreDao.getSingleStore(1);
        JSONObject json = new JSONObject();  
        json.put("aboutUs",singleStore.get("about_us"));  
        json.put("publicNotice",singleStore.get("public_notice"));  //对应json["publicNotice"]
        json.put("picPath",singleStore.get("pic_path"));  

        System.out.println(json.toString());

        HttpServletResponse response = ServletActionContext.getResponse();
        response.setContentType("text/html;charset=utf-8");//一定要写,不然会有乱码
        try {
            response.getWriter().write(json.toString());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
阅读更多
换一批

没有更多推荐了,返回首页