Blog Image

Ajax call using jquery

function makeAjaxRequest(data1,data2,.....,datax){
    $.ajax({
        'type':'POST',
        async: false,
        url: 'http://www.example.com/some_request_url',
        data:{param1:data1,param2:data2,....,paramx:datax},
        success : function(response) {
            console.log(response); // Using this you can check the posted data in browser console window
            return true;// you can process and return the response as required
            //location.reload(); // or you may reload the current page also
        },
        error: function (jqXHR, exception) {
            // If some error is returned, you can check the details below
            var msg = '';
            if (jqXHR.status === 0) {
                msg = 'Not connect.\n Verify Network.';
            } else if (jqXHR.status == 404) {
                msg = 'Requested page 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 = 'Time out error.';
            } else if (exception === 'abort') {
                msg = 'Ajax request aborted.';
            } else {
                msg = 'Uncaught Error.\n' + jqXHR.responseText;
            }
            $('#post').html(msg); // return the error response in your html page or process it further as required
        }
    });
}

 



Author: admin

Vinod Ram has been in Software Industry since 2006 and has experience of over 16 years in Software Development & Project Management domain specialised majorly in LAMP stack & Open Source Technology, building enterprise level Web based Application, Large Database driven and huge traffic Websites and Project Management. He loves to write information articles and blog to share his knowledge and experience with the outside world and help people to find solution for their problems.