標籤:style blog http color get strong
問題:
如下代碼所示,我用AJAX收到來自url: ‘kzkj_check.jsp‘,返回的資料msg,總是包含頁面的html資料,可是我只想要我返回的資料“false”,
$.ajax({ url: ‘kzkj_check.jsp‘, async:false, data: ‘un=‘+ $(‘#userName‘).val() +‘&pw=‘ + $(‘#passWord‘).val(), type: "get", success: function(msg){ if(msg.toString().trim() != "ERROR") //TRUE,return username;Message Sent, check and redirect { // and direct to the success page //alert("name true "+msg.toString().trim()+"=="); $("#msgbox").html(‘Login Verified, Logging in.....‘).addClass(‘myinfo‘).fadeTo(900,1, function() { document.location=‘kzkj_login.jsp?user=‘+msg; }); }
返回的部分AJAX資訊如下:
false<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>xxxxxxxx</title><style type="text/css"><!--body { margin-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; background-color: #999999;}--></style><link href="css/index.css" rel="stylesheet" type="text/css" /></head>
我在url: ‘kzkj_check.jsp‘頁面寫入的資訊只有以下幾句:
if(haveUserInDB == "false") response.getWriter().write("ERROR");else response.getWriter().write("TRUE");
通過以下方式可以直接獲得想要的傳回值,"ERROR"或者"TRUE"。
1.在url: ‘kzkj_check.jsp‘頁面的寫入資訊的方法不要用response,用以下方式:
out.clear();if(haveUserInDB == "false") out.print("ERROR");else out.print(user);
其中out.clear();是將其中的html頁面資訊刪除!
現在可以得到如下AJAX返回資訊:
" ERROR "
包含很多空格,可以在AJAX方法裡這麼寫success: function(msg){ if(msg.toString().trim() != "ERROR")…………}
就可以去除其中的空格,得到想要的傳回值"ERROR"或者"TRUE",就像這樣:
$.ajax({ url: ‘kzkj_check.jsp‘, async:false, data: ‘un=‘+ $(‘#userName‘).val() +‘&pw=‘ + $(‘#passWord‘).val(), type: "get", success: function(msg){ if(msg.toString().trim() != "ERROR") //TRUE,return username;Message Sent, check and redirect { // and direct to the success page //alert("name true "+msg.toString().trim()+"=="); $("#msgbox").html(‘Login Verified, Logging in.....‘).addClass(‘myinfo‘).fadeTo(900,1, function() { document.location=‘kzkj_login.jsp?user=‘+msg; }); }