1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-01-17 22:38:33 +01:00

Allow to encode post/get params with json.stirngify and specify own content-type

This commit is contained in:
Anton Romanov 2015-04-17 17:26:21 -07:00
parent c7972fec13
commit 4c1da6f78b

View File

@ -494,6 +494,7 @@ Tomahawk.ajax = function(url, settings) {
settings.type = settings.type || settings.method || 'get';
settings.method = settings.type;
settings.dataEncodingFormat = settings.dataEncodingFormat || 'form';
if (settings.data) {
var formEncode = function(obj) {
@ -508,14 +509,28 @@ Tomahawk.ajax = function(url, settings) {
return str.join("&");
};
if (typeof settings.data === 'object') {
if (settings.dataEncodingFormat == 'form') {
settings.data = formEncode(settings.data);
settings.contentType = settings.contentType || 'application/x-www-form-urlencoded';
} else if (settings.dataEncodingFormat == 'json') {
settings.data = JSON.stringify(settings.data);
settings.contentType = settings.contentType || 'application/json';
} else {
throw new Error("Tomahawk.ajax: unknown dataEncodingFormat requested: " + settings.dataEncodingFormat);
}
} else {
throw new Error("Tomahawk.ajax: data should be either object or string");
}
if (settings.type.toLowerCase() === 'get') {
settings.url += '?' + formEncode(settings.data);
settings.url += '?' + settings.data;
delete settings.data;
} else {
settings.headers = settings.headers || {};
settings.headers['Content-Type'] = 'application/x-www-form-urlencoded';
settings.data = formEncode(settings.data);
if (!settings.headers.hasOwnProperty('Content-Type')) {
settings.headers['Content-Type'] = settings.contentType;
}
}
}