Saturday, September 12, 2015

How to write ajax method in jquery?

Following snippet shows the ajax method how can we write using JQuery. This method can call as a javascript function also.

//ajax implementation
    function processAjaxForm(_url, element, _data) {
        $.ajax({
            type: "POST",
            data: _data,
            url: _url,
            processData: false,
            dataType: 'json',
            contentType: false,
            success: function (data) {
                if(data.status=='success'){
                   $("#"+element).text(data.message);
                  }else{
                    //if data status as failure
                    var urlval=""+data.message+"";
                    $("#"+element).append(data.message);

                }
            },
            error: function (data) {
//in case of general unexpected error
               $("#"+element).innerHTML=data.message;
            }
        });
    }


Share your thoughts