Subversion Repositories SmartDukaan

Rev

Rev 23340 | Rev 23347 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed


function badRequestAlert(response){
        console.log(response.responseText);
        var errorObject = JSON.parse(response.responseText);
        errorObject = errorObject.response;
        alert('Bad Request\n'+
                        'rejectedType : '+errorObject.rejectedType+'\n'+
                        'rejectedValue : '+errorObject.rejectedValue+'\n'+
                        'message : '+errorObject.message);
}

function internalServerErrorAlert(response){
        console.log(response.responseText);
        var errorObject = JSON.parse(response.responseText);
        errorObject = errorObject.response;
        alert('Internal Server Error\n'+
                'rejectedType : '+errorObject.rejectedType+'\n'+
                'rejectedValue : '+errorObject.rejectedValue+'\n'+
                'message : '+errorObject.message);
}

$( document ).ajaxError(function(event, jqxhr, settings, thrownError) {
        //$( ".log" ).text( "Triggered ajaxError handler." );
        if(jqxhr.status == 400){
                //$('#error-prompt-model').modal();
                badRequestAlert(jqxhr); 
        }else{
                internalServerErrorAlert(jqxhr);
        }
});


function doAjaxRequestWithParamsHandler(urlString, httpType, params, callback_function){
        $.ajax({
           url: urlString,
           async: true,
           cache: false,
           data: params,
       //dataType:'json',
           type: httpType,
           /*beforeSend: function(){
                    $(".loading").show();
           },*/
           success: function(response) {
              //console.log("response"+JSON.stringify(data));
              callback_function(response);
              //$(".loading").hide();
           }
        });
}

function doAjaxRequestWithJsonHandler(urlString, httpType, json, callback_function){
        $.ajax({
           url: urlString,
           async: true,
           cache: false,
           processData: false,
           data: json,
       contentType:'application/json',
           type: httpType,
           /*beforeSend: function(){
                $(".loading").show();
                },*/
           success: function(response) {
              //console.log("response"+JSON.stringify(data));
              callback_function(response);
              //$(".loading").hide();
           }
        });
}


function doAjaxRequestHandler(urlString, httpType, callback_function){
        $.ajax({
           url: urlString,
           async: true,
           cache: false,
           type: httpType,
           /*beforeSend: function(){
                $(".loading").show();
                },*/
           success: function(response) {
              //console.log("response"+JSON.stringify(data));
              callback_function(response);
              //$(".loading").hide();
           }
        });
}

function doAjaxUploadRequestHandler(urlString, httpType, file, callback_function){
        var formData = new FormData();
        formData.append("file", file);
        $.ajax({
           url: urlString,
           type: httpType,
           data: formData,
       dataType: 'json',
       async: true,
       cache: false,
       contentType: false,
       enctype: 'multipart/form-data',
       processData: false,
       /*beforeSend: function(){
                $(".loading").show();
                },*/
           success: function(response) {
              //console.log("response"+JSON.stringify(data));
              callback_function(response);
              //$(".loading").hide();
           }
        });
}

function doAjaxGetDownload(urlString, fileName){
        doAjaxDownload(urlString, "GET", null, fileName);
}

function doAjaxPostDownload(urlString, data, fileName){
        doAjaxDownload(urlString, "POST", data, fileName);
}

function doAjaxDownload(urlString, httpType, data, fileName){
        xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function() {
            var a;
            if (xhttp.readyState === 4 && xhttp.status === 200) {
                // Trick for making downloadable link
                a = document.createElement('a');
                a.href = window.URL.createObjectURL(xhttp.response);
                // Give filename you wish to download
                a.download = fileName;
                a.style.display = 'none';
                document.body.appendChild(a);
                a.click();
            }
            if(xhttp.readyState == 4 && xhttp.status === 400){
                badRequestAlert(xhttp.responseText);
            }
            if(xhttp.readyState == 4 && xhttp.status === 500){
                internalServerErrorAlert(xhttp.responseText);
            }
        };
        // Post data to URL which handles post request
        xhttp.open(httpType, urlString);
        if(httpType == "POST"){
                xhttp.setRequestHeader("Content-Type", "application/json");
        }
        // You should set responseType as blob for binary responses
        xhttp.responseType = 'blob';
        xhttp.send(data);
}

function downloadReport(urlString, httpType, data, fileName){
        
}

function uploadDocument(file){
        var url = '/profitmandi-web/document-upload';
        doAjaxUploadRequestHandler(url, 'POST', file, function(response){
                var documentId = response.response.document_id; 
                console.log("documentId : "+documentId);
                return documentId;
        });
}