Mi aspettavo che AngularJS codificasse i parametri della stringa di query utilizzando la funzione javascript standard encodeURIComponent
. Secondo la seguente prova non è il caso:Angularjs Come codificare i componenti uri
describe('$http', function() {
it('encodes uri components correctly', inject(function($http, $httpBackend) {
var data = 'Hello from http://example.com';
$httpBackend.expectGET('/api/process?data=' + encodeURIComponent(data));
$http({ method: 'GET', url: '/api/process', params: { data: data } });
$httpBackend.flush();
}));
});
Il test ha esito negativo con il seguente errore:
$http encodes uri components correctly
Error: Unexpected request: GET /api/process?data=Hello+from+http:%2F%2Fexample.com
Expected GET /api/process?data=Hello%20from%20http%3A%2F%2Fexample.com
Riassumendo:
- codifica prevista:
Hello%20from%20http%3A%2F%2Fexample.com
- Codifica effettiva:
Hello+from+http:%2F%2Fexample.com
Quale metodo di codifica componente uri (ovvero parametri stringa di query) dovrei aspettarmi con AngularJS?
Possiamo accedervi in qualche modo come angular.encodeUriQuery o qualcosa del genere? –