jQuery 如何将ajax的成功函数的结果与字符串进行比较
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4946485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to compare the result of success function of ajax with a string
提问by Prerna
$.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>
i want to compare the value returned by success function to be compared with a string but the above code is not working i also want to add the css class to the "#result" id. the alert box is not comming.
我想将成功函数返回的值与字符串进行比较,但上面的代码不起作用我还想将 css 类添加到“#result”ID。警报框没有出现。
回答by Adrian Rodriguez
Something there is a blank space. With $.trim() function you can delete all blank spaces. It works!!!
东西有空白。使用 $.trim() 函数可以删除所有空格。有用!!!
$.ajax({
type: "GET",
url: url,
success: function(data) {
var result = $.trim(data);
if(result==="available"){
alert("available");
return false;
}
}
});
回答by Ofure Ukpebor
$.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.');
}
}
//Make sure you use the replace function to strip of any extra spaces
//确保您使用替换功能去除任何多余的空格
回答by Anurag Tiwari
The primary problem in ajax comparison is unwanted space:
ajax 比较的主要问题是不需要的空间:
var result = $.trim(data);
回答by BalusC
You should not print HTML <h1>
element around the ajax response in JSP. Get rid of it. You need to ensure that you have nothingbefore <%
and after %>
in JSP, even no whitespace/newlines. JSP would emit them anyway. Actually, a JSP is the wrong tool for the job. A servlet is a better suit for the job.
您不应该<h1>
在 JSP 中围绕 ajax 响应打印 HTML元素。摆脱它。你需要确保你有什么之前<%
和之后%>
在JSP,甚至没有空格/换行符。无论如何,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);
}
This will work fine with jQuery dataType: text
. Even more, it's not strictly required to set the data type here.
这将适用于 jQuery dataType: text
。更重要的是,这里并不严格要求设置数据类型。
Related questions:
相关问题:
回答by William
Set the dataType to 'text'. If that doesn't work, then make sure that 'available' really is the only thing being returned, with no line endings or spaces or anything.
将数据类型设置为“文本”。如果这不起作用,那么请确保 'available' 确实是唯一返回的东西,没有行尾或空格或任何东西。
回答by andres descalzo
You have to set the data type for ajax response, for example:
您必须为 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
指定 AJAX 请求的数据类型
doc jQuery.Ajax
EDIT
编辑
try this options:
试试这个选项:
// 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");
}
}
});
Ideally, your file "test.jsp" was as follows:
理想情况下,您的文件“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.
%>
the end is based on this link
结尾基于此链接
回答by prakash
success:function(response){
if(response=="yes")
{
myFunction();
}else{
myFunctionn();
}
}