i'm doing a $http.jsonp call in angular against Apple's iTunes Search API to retrieve data about songs from a specific artist.
A normal get request to the endpoint would be
https://itunes.apple.com/search?callback=angular.callbacks._0&term=madonna+ghosttown
See that the callback is introduced by $http.jsonp and it would work correctly. The problem is, that the call encodes the url and replaces '+' with '%2B'. This breaks the Api and returns an empty array.
The call will look like the subsequent then:
https://itunes.apple.com/search?callback=angular.callbacks._0&term=madonna%2Bghosttown
How can i force $http to not substitute the '+' which separates the search terms? Following code block shows my call.
$http.jsonp("http://itunes.apple.com/search", {
params: {
"callback": "JSON_CALLBACK",
"term": "madonna+ghosttown"
}
}).success(function (data, status, headers, config) {
console.log(data);
}).error(function (data, status, headers, config) {
console.log(data);
});
I would really appreciate your help here. Cheers, Ben
I propose two possible solutions.
As I see it, the problem stems from Angular's $httpParamSerializer which automatically encodes URL parameters.
The first solution is to not use the params
key at all and append the URL parameters manually to the URL, like this:
$http.jsonp("http://itunes.apple.com/search?callback=JSON_CALLBACK&term=madonna+ghosttown");
A second solution could be to override the default $httpParamSerializer that is used by Angular's $http (see $http API documentation, chapter "Usage", config key "paramsSerializer"). A (overly simplistic) example would be the following code, which does not encode parameters at all:
$http.jsonp("http://itunes.apple.com/search", {
params: {
"callback": "JSON_CALLBACK",
"term": "madonna+ghosttown"
},
paramsSerializer: function(param) {
return param;
}
});