Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Fixed: format multilevel json to querystring correctly #5885

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 29 additions & 17 deletions src/ng/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -1009,25 +1009,37 @@ function $HttpProvider() {
}
}


function buildUrl(url, params) {
if (!params) return url;
var parts = [];
forEachSorted(params, function(value, key) {
if (value === null || isUndefined(value)) return;
if (!isArray(value)) value = [value];

forEach(value, function(v) {
if (isObject(v)) {
v = toJson(v);
}
parts.push(encodeUriQuery(key) + '=' +
encodeUriQuery(v));
});
function is(type, obj) {
var clas = Object.prototype.toString.call(obj).slice(8, -1);
return obj !== undefined && obj !== null && clas === type;
}

function toQueryString(params, parts, pk) {
forEachSorted(params, function(value, key) {
if (value === null || value === undefined) return;
if (pk) key = pk+'['+key+']';
if (is('Array',value)) {
forEach(value, function(v,i) {
if(is('Object', v)){
key = key+'['+i+']';
toQueryString(v, parts, key);
} else {
parts.push(encodeUriQuery(key) + '=' + encodeUriQuery(v));
}
});
return url + ((url.indexOf('?') == -1) ? '?' : '&') + parts.join('&');
return;
} else if (is('Object',value)) {
return toQueryString(value, parts, key);
}
parts.push(encodeUriQuery(key) + '=' + encodeUriQuery(value));
});
}


function buildUrl(url, params) {
if (!params) return url;
var parts = [];
toQueryString(params, parts);
return url + ((url.indexOf('?') == -1) ? '?' : '&') + parts.join('&');
}
}];
}
2 changes: 1 addition & 1 deletion test/ng/httpSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ describe('$http', function() {


it('should jsonify objects in params map', inject(function($httpBackend, $http) {
$httpBackend.expect('GET', '/url?a=1&b=%7B%22c%22:3%7D').respond('');
$httpBackend.expect('GET', '/url?a=1&b%5Bc%5D=3').respond('');
$http({url: '/url', params: {a:1, b:{c:3}}, method: 'GET'});
}));

Expand Down