Subversion Repositories SmartDukaan

Rev

Rev 23340 | Rev 23347 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
21627 kshitij.so 1
 
22956 ashik.ali 2
function badRequestAlert(response){
3
	console.log(response.responseText);
4
	var errorObject = JSON.parse(response.responseText);
5
	errorObject = errorObject.response;
6
	alert('Bad Request\n'+
7
			'rejectedType : '+errorObject.rejectedType+'\n'+
8
			'rejectedValue : '+errorObject.rejectedValue+'\n'+
9
			'message : '+errorObject.message);
10
}
11
 
12
function internalServerErrorAlert(response){
13
	console.log(response.responseText);
14
	var errorObject = JSON.parse(response.responseText);
15
	errorObject = errorObject.response;
16
	alert('Internal Server Error\n'+
17
		'rejectedType : '+errorObject.rejectedType+'\n'+
18
		'rejectedValue : '+errorObject.rejectedValue+'\n'+
19
		'message : '+errorObject.message);
20
}
21
 
22
$( document ).ajaxError(function(event, jqxhr, settings, thrownError) {
23
	//$( ".log" ).text( "Triggered ajaxError handler." );
24
	if(jqxhr.status == 400){
22982 ashik.ali 25
		//$('#error-prompt-model').modal();
22956 ashik.ali 26
		badRequestAlert(jqxhr);	
27
	}else{
28
		internalServerErrorAlert(jqxhr);
29
	}
30
});
31
 
23026 ashik.ali 32
 
23193 ashik.ali 33
function doAjaxRequestWithParamsHandler(urlString, httpType, params, callback_function){
34
	$.ajax({
35
	   url: urlString,
23343 ashik.ali 36
	   async: true,
23193 ashik.ali 37
	   cache: false,
38
	   data: params,
39
       //dataType:'json',
40
	   type: httpType,
23343 ashik.ali 41
	   /*beforeSend: function(){
42
		    $(".loading").show();
43
	   },*/
23193 ashik.ali 44
	   success: function(response) {
45
	      //console.log("response"+JSON.stringify(data));
46
	      callback_function(response);
23343 ashik.ali 47
	      //$(".loading").hide();
23193 ashik.ali 48
	   }
49
	});
50
}
51
 
23032 ashik.ali 52
function doAjaxRequestWithJsonHandler(urlString, httpType, json, callback_function){
53
	$.ajax({
54
	   url: urlString,
23343 ashik.ali 55
	   async: true,
23032 ashik.ali 56
	   cache: false,
57
	   processData: false,
58
	   data: json,
59
       contentType:'application/json',
60
	   type: httpType,
23343 ashik.ali 61
	   /*beforeSend: function(){
62
	    	$(".loading").show();
63
  		},*/
23032 ashik.ali 64
	   success: function(response) {
65
	      //console.log("response"+JSON.stringify(data));
66
	      callback_function(response);
23343 ashik.ali 67
	      //$(".loading").hide();
23032 ashik.ali 68
	   }
69
	});
70
}
71
 
72
 
23026 ashik.ali 73
function doAjaxRequestHandler(urlString, httpType, callback_function){
22982 ashik.ali 74
	$.ajax({
75
	   url: urlString,
23343 ashik.ali 76
	   async: true,
23026 ashik.ali 77
	   cache: false,
22982 ashik.ali 78
	   type: httpType,
23343 ashik.ali 79
	   /*beforeSend: function(){
80
	    	$(".loading").show();
81
  		},*/
22982 ashik.ali 82
	   success: function(response) {
83
	      //console.log("response"+JSON.stringify(data));
84
	      callback_function(response);
23343 ashik.ali 85
	      //$(".loading").hide();
22982 ashik.ali 86
	   }
87
	});
88
}
89
 
23026 ashik.ali 90
function doAjaxUploadRequestHandler(urlString, httpType, file, callback_function){
22982 ashik.ali 91
	var formData = new FormData();
92
	formData.append("file", file);
93
	$.ajax({
94
	   url: urlString,
95
	   type: httpType,
96
	   data: formData,
97
       dataType: 'json',
23343 ashik.ali 98
       async: true,
22982 ashik.ali 99
       cache: false,
100
       contentType: false,
101
       enctype: 'multipart/form-data',
102
       processData: false,
23343 ashik.ali 103
       /*beforeSend: function(){
104
	    	$(".loading").show();
105
  		},*/
22982 ashik.ali 106
	   success: function(response) {
107
	      //console.log("response"+JSON.stringify(data));
108
	      callback_function(response);
23343 ashik.ali 109
	      //$(".loading").hide();
22982 ashik.ali 110
	   }
111
	});
112
}
113
 
23343 ashik.ali 114
function doAjaxGetDownload(urlString, fileName){
115
	doAjaxDownload(urlString, "GET", null, fileName);
22982 ashik.ali 116
}
117
 
23343 ashik.ali 118
function doAjaxPostDownload(urlString, data, fileName){
119
	doAjaxDownload(urlString, "POST", data, fileName);
21627 kshitij.so 120
}
121
 
23343 ashik.ali 122
function doAjaxDownload(urlString, httpType, data, fileName){
22488 ashik.ali 123
	xhttp = new XMLHttpRequest();
124
	xhttp.onreadystatechange = function() {
125
	    var a;
126
	    if (xhttp.readyState === 4 && xhttp.status === 200) {
127
	        // Trick for making downloadable link
128
	        a = document.createElement('a');
129
	        a.href = window.URL.createObjectURL(xhttp.response);
130
	        // Give filename you wish to download
23343 ashik.ali 131
	        a.download = fileName;
22488 ashik.ali 132
	        a.style.display = 'none';
133
	        document.body.appendChild(a);
134
	        a.click();
135
	    }
22956 ashik.ali 136
	    if(xhttp.readyState == 4 && xhttp.status === 400){
137
	    	badRequestAlert(xhttp.responseText);
138
	    }
139
	    if(xhttp.readyState == 4 && xhttp.status === 500){
140
	    	internalServerErrorAlert(xhttp.responseText);
141
	    }
22488 ashik.ali 142
	};
143
	// Post data to URL which handles post request
23343 ashik.ali 144
	xhttp.open(httpType, urlString);
145
	if(httpType == "POST"){
146
		xhttp.setRequestHeader("Content-Type", "application/json");
147
	}
22488 ashik.ali 148
	// You should set responseType as blob for binary responses
149
	xhttp.responseType = 'blob';
150
	xhttp.send(data);
151
}
152
 
23343 ashik.ali 153
function downloadReport(urlString, httpType, data, fileName){
23026 ashik.ali 154
 
22523 ashik.ali 155
}
156
 
23343 ashik.ali 157
function uploadDocument(file){
158
	var url = '/profitmandi-web/document-upload';
159
	doAjaxUploadRequestHandler(url, 'POST', file, function(response){
160
		var documentId = response.response.document_id; 
161
		console.log("documentId : "+documentId);
162
		return documentId;
21987 kshitij.so 163
	});
164
}