解决Ajax返回数据包含整个jsp页面的问题

处理请求页

<%
    ResultSet rs = conn.executeQuery("select name from tb_book order by id desc");
    String str = "";
    str = "{ \"info\":\"";
    if(rs.next()){
        do{
            str += ""+rs.getString(1)+"";
        }while(rs.next());
    }else{
        str += "暂无图书信息";
    }
    str += "\" }";
    out.clear();  // 清除前面的html标签
    out.print(str);
    out.close(); // 清除后面的html标签
%>

发起请求页:


自定义封闭ajax脚本函数:

var AjaxRequest = function(obj){
    this.req = new XMLHttpRequest();
    this.req.open(obj.type,obj.url,true);
    if(obj.type=="POST"){
        this.req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    }
    this.req.send();
    this.req.onreadystatechange = function(){
        if(this.req.readyState==4){
            if(this.req.status==200){
                if(obj.dataType=='json'){
                    obj.success(JSON.parse(this.req.responseText));
                }else{
                    obj.success(this.req.responseText);
                }
            }else{
                obj.error({status:this.req.status,statusText:this.req.statusText});
            }
        }
    }.bind(this);
}

转载于:https://blog.51cto.com/maplebb/2296163

你可能感兴趣的:(解决Ajax返回数据包含整个jsp页面的问题)