Coding Duchess
Coding Duchess

Reputation: 6909

Escaping forward slash in javascript when passing parameters to the url

I am sending a request to a Web API from my javascript code as follows:

http://localhost:49358/api/myClasses/GetMyData/first%20second/third%20fourth%2Ffifth

where

http://localhost:49358/api/myClasses/GetMyData/

is my controller method to which i am passing two parameters first second and third fourth/fifth

A whitespace gets escaped automatically by %20 automatically and / I escape using %2F

The issues is that the url call above returns error 404. Meanwhile if I replace third fourth/fifth by any valid value not containing forward slash, everything works just fine.

Could anyone point me in the right direction?

My controller code is below:

(function () {
  angular.module("myApp").controller("MyController", ["$scope", "$state", "$http", MyFunction]);

  function MyFunction($scope, $state, $http) {
    $scope.MyClass1=[];
    $http.get('http://localhost:49358/api/myClasses/GetMyData/' + $scope.$parent.param1 + '/' + encodeURIComponent($scope.$parent.param2))
      .then(function (result) {
        $scope.MyClass1 = result.data;
    });
  };
})();

Upvotes: 1

Views: 2857

Answers (1)

Coding Duchess
Coding Duchess

Reputation: 6909

Turns out it is not a client-side issue, as I encoded the slash. The real issue is that the server cannot handle the slash in the parameter in the url. I have decided to replace the slash with underscore and modify my Web API to replace all the underscores with a forward slash later on. Any other suitable character can be used as well

Upvotes: 1

Related Questions