responseText包含多余的空格字符(换行符,换行符),如何在jsp中防止和删除它们?

时间:2019-04-19 08:41:57

标签: ajax jsp

responseText contains extra whitespace characters (new lines, line feeds), how to prevent and remove them?

请阅读以上链接

我的jsp页面发送“好” “不是” ,包括空格。 如何防止和删除空格或换行符

valid.js

window.onload=initPage;
function initPage(){
    alert("Browser Started");
    //call checkUsername function
     document.getElementById("username").onblur=checkUsername;
 }
function checkUsername(){
    request=createRequest();
    if(request==null){
        alert("Unable to create request");
    }else{
        alert("Request Object Created")
         var theName = document.getElementById("username").value;
         var username = escape(theName);
         //create url pass username
         var url= "checkName.jsp?username=" + username;
         //any change execute showUsernameStatus function
         request.onreadystatechange = showUsernameStatus;
         request.open("GET", url, true);
         request.send(null);
    }
}
function showUsernameStatus(){
    if (request.readyState == 4) {
        if (request.status == 200) {
            var val=request.responseText;
            alert("Response: "+val);
             if (val == "okay") {
                    //something
                  } else {
                    //something
                  }
        }
    }
}

checkName.jsp

<%@ page language="java"
    pageEncoding="UTF-8"%>
<%String username=request.getParameter("username");
    System.out.println("Username:"+username);
    if(!username.equals("") && username!=null){
        if(!username.equals("Raju")){
            out.print("okay");
            System.out.println("okay");
        }else{
            out.println("not");
            System.out.println("not");
        }
    }
%>

2 个答案:

答案 0 :(得分:1)

首先,我鼓励您使用JSON对象进行此类工作。

Here是一个很好的示例,说明如何在JSP页面中使用JSON。

现在,对于您的情况,您必须注意,以修剪从JSP页面获取的结果。

您必须在JavaScript函数中使用trim。

 if (val.trim() == "okay") {
     //something
 } else {
     //something
 }

或者您可以使用正则表达式删除所有空格:

var newVal = val.replace(/\s+/, "");

答案 1 :(得分:1)

您可以在Javascript函数中使用trim()

//console.log(json);
function score(json){
var customer = json.fields.customer != null && json.fields.customer.length > 0 ? json.fields.customer[0].value : null;
//console.log(customer)
    var score3 = 0;
        if(typeof customer == 'string'){          
            if(customer === "Yes"){
                score3 = +10;
            }
            else if(customer === "No"){
                score3 = +5;
            }
        }
        else{
            score3 = 0;
        }
        return score3;
}

var json = {
  fields:{
     customers:null
  }
};
        
console.log(score(json));

json = {
  fields:{
     customer:[{
      id: 1,
      value: 'Yes'
     }]
  }
};
//console.log(json)
console.log(score(json));

json = {
  fields:{
     customer:[{
      id: 1,
      value: 'No'
     }]
  }
};
console.log(score(json));

或者您可以在jsp页面中使用trimDirectiveWhitespaces=true

var val=request.responseText;   
if (val.trim() == "okay") {
    // something
} else {
    // something
}