Subversion Repositories SmartDukaan

Rev

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

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