Loading...

jQuery-Ajax-Standard Usage Guidelines

jQuery-Ajax-Standard Usage Guidelines

Often developer uses jQuery & AJAX to update parts of a web page or send data to check some business logic, without reloading the whole page (also known as Asynchronous Update).

We can handle proper success and error message as per the server’s response using jqXHR, exception parameters.

Have a look at the demo jQuery Ajax function to check the user’s login.

function login_validation()
{

var email = $(“#email”).val();
var password = $(“#password”).val();
var url = “login-url-to-check”;

$.ajax({

url: url,
type: “post”
data: {’email’:email,’password’:password},
dataType:”json”,
success:function(data)
{

alert(‘Login Successful’);
window.location.href = “after-login-success-page”;
return false;

},
error: function (jqXHR, exception)
{

var msg = ”;
if (jqXHR.status == 404) {
msg = ‘Requested page is not found. [404]’;
} else if (jqXHR.status == 500) {
msg = ‘Internal Server Error [500].’;
} else if (exception === ‘parsererror’) {
msg = ‘Requested JSON parse failed.’;
} else if (exception === ‘timeout’) {
msg = ‘A time out error occured. Please try again’;
} else if (exception === ‘abort’) {
msg = ‘Ajax request aborted.’;
} else {
msg = ‘Uncaught Error.\n’ + jqXHR.responseText;
}
alert(msg);

},
timeout: 8000

});

return false;

}

 

 

Follow my other blogs here