0

I use Angular in both websites. I have a first website with a JSON in a controller. I need to send it to another website's controller.

Question : The following code working good as far i don't send special characteres in the JSON like a & who will of course broke my url ( and of course the url length limitation )

Example of JSON who broke my code : {"recordedby":"mackee&","year":2016}

how can i send special caracteres like & or / without breaking the url ?

I looked for a solution to send data as post but i don't find anything.


What i do now :

In first website controller :

angular.module('site1').controller('site1Controller', ['$scope',
    function($scope) {
        ...
        $scope.createEditLink =function(){
            var query = encodeURIComponent(JSON.stringify(jsonToSend));
            var url =  host +"/edit/query="+query;
            return url ;
        }
        ...
    }
]);

In first website view :

<a ng-href="{{createEditLink()}}" target="_blank">
   edit
</a>

The JSON To send :

{"filtered":{"filter":{"and":[{"or":[{"regexp":{"recordedby":".*mackee*.*"}},{"term":{"recordedby":"mackee*","_cache":false}}]}]}}}

so on click it open this link :

http://host/edition/query={%22filtered%22:{%22filter%22:{%22and%22:[{%22or%22:[{%22regexp%22:{%22recordedby%22:%22.*mackee*.*%22}},{%22term%22:{%22recordedby%22:%22mackee*%22,%22_cache%22:false}}]}]}}}

In the second website's controller :

angular.module('site2').controller('site2Controller', ['$scope','$routeParams',
    function($scope,$routeParams) {

        function myUrlToArray(url) {
           var paramsFormatted = {};
           var paramstmp = decodeURI(url).split("&");
           for (var index = 0; index < paramstmp.length; ++index) {
              var tmp = paramstmp[index].split("=");
              paramsFormatted[ tmp[0] ] = tmp[1];
           }
           return paramsFormatted;
        }

        $scope.urlParams = myUrlToArray($routeParams.params);       
        var myJson = JSON.parse(decodeURIComponent($scope.urlParams["query"]));

         ....
    }
]);

Any suggestions are welcomed.

AlainIb
  • 4,544
  • 4
  • 38
  • 64

1 Answers1

2

The reason that you see such a URl is because it is being encoded according to the following encoding rules :

  • The alphanumeric characters "a" through "z", "A" through "Z" and "0" through "9" remain the same.
  • The special characters ".", "-", "*", and "_" remain the same.
  • The space character " " is converted into a plus sign "+"
  • All other characters are unsafe and are first converted into one or more bytes using some encoding scheme. Then each byte is represented by the 3-character string "%xy", where xy is the two-digit hexadecimal representation of the byte

Source: How to handle special characters in url as parameter values?

Also, the & is interpreted as HTML and must be HTML encoded.Therefore you must replace & with &amp; in your json for it to work correctly

Community
  • 1
  • 1
Avantika Saini
  • 792
  • 4
  • 9