无法通过ajax调用发送Crypto JS加密数据

时间:2015-11-02 07:26:40

标签: ajax angularjs http cryptojs

我正在尝试加密密码并通过Ajax呼叫帖将其存储在Mongo LAB上。 我收到以下错误: 我搜索了错误,但没有得到它意味着它循环指向的意思。 错误:

TypeError: Converting circular structure to JSON
    at Object.stringify (native)
    at n.$scope.signup (file:///C:/Users/naval.joshi/Desktop/eWallet/Assignment/Assignment/register.js:26:32)
    at fn (eval at <anonymous> (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js:212:409), <anonymous>:4:209)
    at f (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js:253:485)
    at n.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js:133:221)
    at n.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js:133:451)
    at HTMLButtonElement.<anonymous> (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js:254:36)
    at HTMLButtonElement.Hf.c (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js:35:217)

代码:

var reg = angular.module('register', ['ngRoute']);
reg.controller('regCntrl', function($scope,$http) {
     $scope.signup = function() {

        $scope.encryptedData = CryptoJS.AES.encrypt($scope.password, "123"); 
        alert($scope.encryptedData);

        //alert("naval");

                      var inventory = {
                        name: $scope.name ,
                        email: $scope.email,
                        password: $scope.encryptedData,
                        phoneno:$scope.phoneno
                          };
            $.ajax({
                url: "https://api.mongolab.com/api/1/databases/geolocation/collections/boom?apiKey=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
                type: "POST",
                data: JSON.stringify( inventory ),
                contentType: "application/json; charset=utf-8"
            }).done(function( msg ) {
                console.log(msg);
            });
 };
    // populate the code to fill mongo DB
});

1 个答案:

答案 0 :(得分:1)

我认为(与How to pass encrypted data via browser (HTML5) session variable类似)的修补程序是将.toString()添加到加密对象中。因此,请将加密线更改为:

$scope.encryptedData = CryptoJS.AES.encrypt($scope.password, "123").toString(); 

同样只是提示,而不是alert值,console.log将显示您正在处理的变量的实际值是什么)