0

I am newbie in ajax.i want to send a normal ajax request when i will click the search button.i write some code the search onclick function..

$(document).ready(function() {
    $("#Search").click(function() {
        $.ajax({
            "dataType": 'json',
            "type": "POST",
            "url": "../test/loginform_details.do",
            "data": {

                username: $('#username').val().toString(),
                password: $('#password').val().toString(),

            },
            "success": function(data) {
                fnCallback(data);
            }
        });
    });
});

will that be ok??sholud i add fnCallback(data) in the success :function(data)??plz give some idea..

3
Contributors
2
Replies
12
Views
2 Years
Discussion Span
Last Post by gabrielcastillo
0

In order to see if your Ajax transport works as expected, you can do this - quick n dirty:

"success": function(data) {
     alert(JSON.stringify(data));
}

Once you're satisfied that the data response is what you're expecting, sure pass it to a function of your choice - or write inline code within the function(data).

0

If you are using this script to log someone into a session, then I would think you would want to use a call back to notify the user of success or failure. Also, you would want to redirect the set a cookie for the user. All will require a callbackfunction.

For instance:

success: function(json){
    if( json.success ){
        window.location.href="/my_account";
        return false;
    }

    if( json.error ){
        alert(json.error);
        return false;
    }

    if( json.has_message ){
        html = '<div id="msg" style="display:block; border:1px solid red; background-color:pink; color:red; padding:5px 10px;>'+json.has_message+'</div>';
        $(html).prependTo('body');
        return false;
    }
}

Above is just and example of the top of my head. not tested. The point is, getting a json return and doing something with it for the user.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.