$.ajax({
type: "post",
url: "test.jsp",
data: "user="+name.val(),
success: function(msg) {
$('#result').hide();
$("#result").html(msg)
.fadeIn("slow");
if( msg =="available")
{
alert(msg);
}
}
});
test.jsp
<h1>
<%
String user=request.getParameter("user");
if(user.equals("prerna"))
out.print("available");
else
out.print("not available");
%>
</h1>
我想比较成功函数返回的值与字符串进行比较 但上面的代码不起作用 我还想将css类添加到“#result”id。 警报框未来。
答案 0 :(得分:17)
有空格的东西。使用$ .trim()函数,您可以删除所有空格。它有效!!!
$.ajax({
type: "GET",
url: url,
success: function(data) {
var result = $.trim(data);
if(result==="available"){
alert("available");
return false;
}
}
});
答案 1 :(得分:3)
$.ajax({
dataType: "text",
url : 'saveDeviceLike.php',
success : function(data){
var reply = data.replace(/\s+/, ""); //remove any trailing white spaces from returned string
if (reply == 'success')
$('#pleaseWait').html('Your have successfully registered for this competition.<br/>You will be contacted via mail.');
if (reply == 'registered')
$('#pleaseWait').html('You have already been registered for the competition!');
if (reply == 'failed')
$('#pleaseWait').html('Your registration cannot be completed now.<br/>Please try again later.');
}
//确保使用replace函数去除任何多余的空格
答案 2 :(得分:2)
ajax比较中的主要问题是不需要的空间:
var result = $.trim(data);
答案 3 :(得分:1)
您不应该在JSP中围绕ajax响应打印HTML <h1>
元素。摆脱它。您需要确保在<%
之前和之后%>
之前 ,甚至没有空格/换行符。 JSP无论如何都会发出它们。实际上,JSP是这项工作的错误工具。 servlet更适合这项工作。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String user = request.getParameter("user");
String message = "not available";
if ("prerna".equals(user)) {
message = "available";
}
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(message);
}
这适用于jQuery dataType: text
。更重要的是,这里不一定要设置数据类型。
答案 4 :(得分:0)
将dataType设置为“text”。如果这不起作用,那么确保'available'确实是唯一返回的东西,没有行结尾或空格或任何东西。
答案 5 :(得分:0)
您必须为ajax响应设置数据类型,例如:
// text
$.ajax({
dataType: "text",
type: "post",
url: "test.jsp",
data: "user="+name.val(),
success: function(msg) {
$("#result")
.hide();
.html(msg)
.fadeIn("slow");
if(msg == "available") {
alert("is available");
}
}
});
// json
$.ajax({
dataType: "text",
type: "post",
url: "test.jsp",
data: "user="+name.val(),
success: function(data) {
$("#result")
.hide();
.html(data.msg)
.fadeIn("slow");
if(data.msg == "available") {
alert("is available");
}
}
});
Specifying the Data Type for AJAX Requests
doc jQuery.Ajax
修改强>
尝试以下选项:
// text
$.ajax({
dataType: "text",
type: "post",
url: "test.jsp",
data: "user="+name.val(),
success: function(msg) {
$("#result")
.hide();
.html(msg)
.fadeIn("slow");
if(msg.indexOf("not available") > -1) {
alert("not available");
}
else if(msg.indexOf("available") > -1) {
alert("available");
}
}
});
// html
$.ajax({
dataType: "html",
type: "post",
url: "test.jsp",
data: "user="+name.val(),
success: function(data) {
$("#result")
.hide();
.html(data)
.fadeIn("slow");
if($(data).filter(":contains(not available)").length > 0) {
alert("not available");
}
else if($(data).filter(":contains(available)").length > 0) {
alert("available");
}
}
});
理想情况下,您的文件“test.jsp”如下:
<%
String user=request.getParameter("user");
if(user.equals("prerna"))
out.print("available");
else
out.print("not available");
out.flush(); // Send out whatever hasn't been sent out yet.
out.close(); // Close the stream. Future calls will fail.
return; // Return from the JSP servelet handler.
%>
结尾基于this link
答案 6 :(得分:0)
success:function(response){
if(response=="yes")
{
myFunction();
}else{
myFunctionn();
}
}