3

im getting json object from the api , in that object one field is encoded with base 64 format. after getting the response i need to decode the base64 data and need to show in the plain text. sample data

{"id":33132,"dataFormat":"TEVOOjA="}//base64 to ascii i.e LEN:0

desired output - LEN:0

 <script>
            var app = angular.module('myApp', []);
            app.controller('myCtrl', function ($scope, $http) {
                $http.get('url', {
                    headers: { 'Authorization': 'Basic a2Vybyt==' }
                })
                    .then(function (response) {
                   $scope.names = response.data;
                   $scope.EncodedData = names.dataFrame;
                   $scope.decodedFrame = atob(EncodedData);
                });
            });
    </script>
<h2>{{names.decodedFrame }}</h2>
1

3 Answers 3

10
var string = 'Hello World!';

// Encode the String

var encodedString = btoa(string);
console.log(encodedString); // Outputs: "SGVsbG8gV29ybGQh"

// Decode the String

var decodedString = atob(encodedString);
console.log(decodedString); // Outputs: "Hello World!"

in Angular :

HTML :

<div ng-app ng-controller="LoginController">
  <div>encoded jsonData.dataFormat : {{ jsonData.dataFormat }}</div>
  <div>decoded jsonData.dataFormat : {{ decodedFrame }}</div>
</div>

JavaScript :

function LoginController($scope) {
  $scope.jsonData = {
    "id": 33132,
    "dataFormat": "TEVOOjA="
  };
  $scope.decodedFrame = atob($scope.jsonData.dataFormat)
}

JSFiddle : https://jsfiddle.net/nikdtu/2pwauuLu/

Sign up to request clarification or add additional context in comments.

5 Comments

i dnt have original text, i have to decode only encoded text
@Swapna: Use only Decode portion of above suggested code. encodedString = "TEVOOjA="; var decodedString = atob(encodedString); console.log(decodedString);
thanks. how to send single value $scope.decodedFrame = atob(names.dataFrame);im using json object. in that one field is dataFrame that i need to decode
thanks a lot @nikhil
@Swapna : Your Welcome !
0

Try using a base64 encoder/decoder like this one.

Comments

0

s = atob("TEVOOjA=");
console.log(s); //LEN:0

See MDN Web API Reference -- window.atob()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.