52,804
社区成员




@RequestMapping(value="/yonghu.do",method=RequestMethod.GET)
public void judgeyouxiang(HttpServletRequest request,HttpServletResponse response)throws Exception{
String yonghu = request.getParameter("yonghu");
System.out.println("获取到界面上传来的参数为"+yonghu);
response.setCharacterEncoding("utf-8");
//新建一个printWriter对象
PrintWriter pw = null;
//通过response 获取pw
pw=response.getWriter();
//System.out.println("获得的对象是"+user);
JSONObject resultJSON=resultJson(yonghu);
//用printWritter输出JSON
pw.print(resultJSON.toString());
System.out.println("json="+resultJSON.toString());
pw.flush();
pw.close();
}
/**
* 返回json类型的对象
* @param yonghu
* @return
*/
public JSONObject resultJson(String yonghu){
//定义一个json类型的对象
JSONObject resultJSON = new JSONObject();
if(this.user.findUser(yonghu)==null){
//用pw对象传递json
resultJSON.put("result", "1");
}else{
resultJSON.put("result", "2");
}
return resultJSON;
}
function check()
{
$.ajax(
{
type:"GET",
url:"/yonghu.do",
data:{username:$('#username').val()},
dataType:"json",
async:false,
success:function(data)
{
alert(data);
if(data.result=="1")
{
alert("用户名可用");
$("#spaName").html("<font color=green>可以使用</font>");
}else if(data.result=="2")
{
alert("用户名不可用");
$("#spaName").html("<font color=red>不可使用</font>");
}else{
alert("你错了");
}
},
error:function()
{
alert("加载失败!");
}
});
}
function checkN(){
$val = $("#username").attr("value");
if($val == ""){
alert('用户名不能为空');
return false;
}
return true;
}
</script>
alert(data);//这里输出什么?不是Object,而是json字符串内容?如果是再eval一次生成json对象
data=eval('('+data+')')