-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompat.js
79 lines (67 loc) · 2.77 KB
/
compat.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
angular.module('compat', [])
.factory('plainFormSerializer', function () {
return {
// I serialize the given Object into a key-value pair string. This
// method expects an object and will default to the toString() method.
// --
// NOTE: This is an altered version of the jQuery.param() method which
// will serialize a data collection for Form posting.
// --
// https://github.com/jquery/jquery/blob/master/src/serialize.js#L45
// Quelle: http://www.bennadel.com/blog/2615-posting-form-data-with-http-in-angularjs.htm
serializeData: function (data) {
// If this is not an object, defer to native stringification.
if (!angular.isObject(data)) {
return((data == null) ? '' : data.toString());
}
var buffer = [];
// Serialize each key in the object.
for (var name in data) {
if (!data.hasOwnProperty(name)) {
continue;
}
var value = data[name];
buffer.push(encodeURIComponent(name) + '=' + encodeURIComponent((value == null) ? '' : value));
}
// Serialize the buffer and clean it up for transportation.
return buffer.join('&').replace(/%20/g, '+');
}
}
})
.factory('plainTransformRequest', ['plainFormSerializer', function (serializer) {
function transformRequest(data, getHeaders) {
var headers = getHeaders();
headers['Content-type'] = 'application/x-www-form-urlencoded; charset=utf-8';
return(serializer.serializeData(data));
}
return transformRequest;
}])
.factory('jQueryTransformRequest', function () {
function transformRequest(data, getHeaders) {
var headers = getHeaders();
headers['Content-type'] = 'application/x-www-form-urlencoded; charset=utf-8';
return($.param(data));
}
return transformRequest;
})
.config(function ($httpProvider) {
//Quelle: http://stackoverflow.com/questions/12190166
$httpProvider.defaults.transformRequest = function (data) {
if (data === undefined) {
return data;
}
return $.param(data);
}
})
;
//Quelle: http://stackoverflow.com/questions/12190166
//var transform = function (data) {
// return $.param(data);
//}
//
//$http.post("/foo/bar", requestData, {
// headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
// transformRequest: transform
//}).success(function (responseData) {
// //do stuff with response
//});