如何去除AJAX收到資料中包含的html頁面資料

來源:互聯網
上載者:User

標籤: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;               });          }

 

 

 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.