$.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.
The ajaxStop() method specifies a function to run when ALL AJAX requests have completed. When an AJAX request completes, jQuery checks if there are any more AJAX requests. The function specified with the ajaxStop() method will run if no other requests are pending.
AJAX - Server Response The XMLHttpRequest object has an in-built XML parser. The responseXML property returns the server response as an XML DOM object.
You can store your promise, you can pass it around, you can use it as an argument in function calls and you can return it from functions, but when you finally want to use your data that is returned by the AJAX call, you have to do it like this: promise. success(function (data) { alert(data); });
Definition and Usage The ajaxSuccess() method specifies a function to be run when an AJAX request is successfully completed. Note: As of jQuery version 1.8, this method should only be attached to document.
Something there is a blank space. With $.trim() function you can delete all blank spaces. It works!!!
$.ajax({
type: "GET",
url: url,
success: function(data) {
var result = $.trim(data);
if(result==="available"){
alert("available");
return false;
}
}
});
$.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
The primary problem in ajax comparison is unwanted space:
var result = $.trim(data);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With