1
|
/**
|
2
|
* Java script to allow sending HTTP POST request from normal anchor elements.
|
3
|
*
|
4
|
* expected html:
|
5
|
*
|
6
|
* <a class="http-POST-link" data-cdm-http-post=""
|
7
|
*/
|
8
|
jQuery(document).ready(function($) {
|
9
|
|
10
|
$('.http-POST-link').click(function(eventObject){
|
11
|
eventObject.preventDefault();
|
12
|
var target = $(eventObject.target);
|
13
|
|
14
|
var postData = target.attr('data-cdm-http-post');
|
15
|
var url = target.attr('href');
|
16
|
var target = target.attr('target');
|
17
|
|
18
|
////////////////
|
19
|
/*
|
20
|
var contentType = target.attr('type');
|
21
|
if(!contentType) {
|
22
|
contentType = 'application/x-www-form-urlencoded';
|
23
|
}
|
24
|
|
25
|
$.ajax({
|
26
|
type: 'POST',
|
27
|
dataType : 'json',
|
28
|
dataFilter : function(data, type) {
|
29
|
altert(type);
|
30
|
return data;
|
31
|
},
|
32
|
url: url,
|
33
|
// headers: {
|
34
|
// "Content-Type": contentType
|
35
|
// },
|
36
|
contentType: contentType,
|
37
|
data: postData,
|
38
|
complete: function(jqXHR, textStatus){
|
39
|
alert(textStatus);
|
40
|
var data = jqXHR.responseText;
|
41
|
var win = window.open();
|
42
|
win.document.write(data);
|
43
|
},
|
44
|
|
45
|
});
|
46
|
*/
|
47
|
////////////////////
|
48
|
var postForm = $('<form id="http-method-link-form" action="' + url + '" method="POST" target="' + target + '"></form>');
|
49
|
|
50
|
// extract request parameters from post_data
|
51
|
var tokens = postData.split("&");
|
52
|
for(var i=0; i < tokens.length; i++){
|
53
|
pair = tokens[i].split('=');
|
54
|
postForm.append('<input type="hidden" name="' + pair[0] + '" value="' + urldecode(pair[1]) + '" />');
|
55
|
}
|
56
|
|
57
|
$('body').append(postForm);
|
58
|
postForm.submit();
|
59
|
$('#http-method-link-form').remove();
|
60
|
postForm = null;
|
61
|
|
62
|
});
|
63
|
|
64
|
function urldecode (str) {
|
65
|
return decodeURIComponent((str + '').replace(/\+/g, '%20'));
|
66
|
}
|
67
|
});
|