Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to compare the result of success function of ajax with a string

Tags:

jquery

jsp

 $.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.

like image 209
Prerna Avatar asked Feb 09 '11 14:02

Prerna


People also ask

How do I know if AJAX request is successful?

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.

Which is used to get response of an AJAX call as a string?

AJAX - Server Response The XMLHttpRequest object has an in-built XML parser. The responseXML property returns the server response as an XML DOM object.

How do I return from AJAX success?

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); });

What is the use of success function in AJAX?

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.


3 Answers

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;
    }
    }
});
like image 108
Adrian Rodriguez Avatar answered Oct 22 '22 19:10

Adrian Rodriguez


$.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

like image 3
Ofure Ukpebor Avatar answered Oct 22 '22 19:10

Ofure Ukpebor


The primary problem in ajax comparison is unwanted space:

var result = $.trim(data);
like image 2
Anurag Tiwari Avatar answered Oct 22 '22 20:10

Anurag Tiwari