Subversion Repositories SmartDukaan

Rev

Rev 23347 | Rev 23379 | 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';
23377 ashik.ali 107
	doAjaxUploadRequestHandler(url, 'POST', file, function(response){
108
		var documentId = response.response.document_id; 
109
		console.log("documentId : "+documentId);
110
		return documentId;
111
	});
23347 ashik.ali 112
}
113
 
23377 ashik.ali 114
function doAjaxGetDownload(urlString, fileName){
115
	console.log("fileName : "+fileName);
23343 ashik.ali 116
	doAjaxDownload(urlString, "GET", null, fileName);
22982 ashik.ali 117
}
118
 
23377 ashik.ali 119
function doAjaxPostDownload(urlString, data, fileName){
23343 ashik.ali 120
	doAjaxDownload(urlString, "POST", data, fileName);
21627 kshitij.so 121
}
122
 
23343 ashik.ali 123
function doAjaxDownload(urlString, httpType, data, fileName){
22488 ashik.ali 124
	xhttp = new XMLHttpRequest();
125
	xhttp.onreadystatechange = function() {
126
	    var a;
127
	    if (xhttp.readyState === 4 && xhttp.status === 200) {
128
	        // Trick for making downloadable link
129
	        a = document.createElement('a');
130
	        a.href = window.URL.createObjectURL(xhttp.response);
131
	        // Give filename you wish to download
23343 ashik.ali 132
	        a.download = fileName;
22488 ashik.ali 133
	        a.style.display = 'none';
134
	        document.body.appendChild(a);
135
	        a.click();
136
	    }
22956 ashik.ali 137
	    if(xhttp.readyState == 4 && xhttp.status === 400){
23377 ashik.ali 138
	    		badRequestAlert(xhttp.responseText);
22956 ashik.ali 139
	    }
140
	    if(xhttp.readyState == 4 && xhttp.status === 500){
23377 ashik.ali 141
	    		internalServerErrorAlert(xhttp.responseText);
22956 ashik.ali 142
	    }
22488 ashik.ali 143
	};
144
	// Post data to URL which handles post request
23343 ashik.ali 145
	xhttp.open(httpType, urlString);
146
	if(httpType == "POST"){
147
		xhttp.setRequestHeader("Content-Type", "application/json");
148
	}
22488 ashik.ali 149
	// You should set responseType as blob for binary responses
150
	xhttp.responseType = 'blob';
151
	xhttp.send(data);
23347 ashik.ali 152
}