Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在使用Desive angular时正确获取错误消息(json)_Javascript_Ruby On Rails_Angularjs_Devise - Fatal编程技术网

Javascript 如何在使用Desive angular时正确获取错误消息(json)

Javascript 如何在使用Desive angular时正确获取错误消息(json),javascript,ruby-on-rails,angularjs,devise,Javascript,Ruby On Rails,Angularjs,Devise,我正在尝试使用角度来设置设备。我正在使用angular Desive gem,我使注册过程正常工作。我无法工作的是显示从服务器返回的设计错误消息 我正在成功获取users.json,但是如何获取它并在视图中显示它呢?下面是使用angular Desive方法进行注册的JS代码 有一个函数(error)行,如果出现任何错误,将调用该行。我试着在那里放一条警告信息,它也会像预期的那样被触发 但是如何从这里获取JSON呢 下面是从服务器收到的JSON响应。我只需要在视图中插入这个 users.json

我正在尝试使用角度来设置设备。我正在使用angular Desive gem,我使注册过程正常工作。我无法工作的是显示从服务器返回的设计错误消息

我正在成功获取users.json,但是如何获取它并在视图中显示它呢?下面是使用angular Desive方法进行注册的JS代码

有一个
函数(error)
行,如果出现任何错误,将调用该行。我试着在那里放一条警告信息,它也会像预期的那样被触发

但是如何从这里获取JSON呢

下面是从服务器收到的JSON响应。我只需要在视图中插入这个

users.json

{ 
  "errors": { 
    "email": ["is invalid"],
    "password":["is too short (minimum is 8 characters)"]
  }
}
以下是我的JavaScript:

angular
    .module('checkDevise', ['ngMaterial', 'ngMessages', 'Devise'])
    .controller('DemoCtrl', function($scope, Auth) {
        $scope.submit = function() {
            var credentials = {
                email: $scope.user.email,
                password: $scope.user.password,
                password_confirmation: $scope.user.password_confirmation
            };

            var config = {
                headers: {
                    'X-HTTP-Method-Override': 'POST'
                }
            };

            Auth.register(credentials, config).then(function(registeredUser) {
                console.log(registeredUser); // => {id: 1, ect: '...'}
            }, function(error) {
                // Registration failed...
                //This actually triggers when there is an error and i think its here to get the json????????

            });

            $scope.$on('devise:new-registration', function(event, user) {
                $scope.user = user;
            });
        };
    });
更新:

服务器正在以json的形式发送错误详细信息,说明用户无法保存的原因(如无效密码或电子邮件或已使用的电子邮件地址…)。正如@Rodmentou I added
$scope.error=RESPONSE\u error并在视图中引用它,如{{error}。它现在在视图中显示错误,如下所示

当前视图中显示的错误

{"data":
  {"errors": 
    {
      "email": ["has already been taken"],
      "password_confirmation":["doesn't match Password"]
    }
  }, 
  "status":422,
  "config": 
    {
      "method":"POST",
      "transformRequest":[null],
      "transformResponse":[null],
      "url":"/users.json",
      "data": 
        {
         "user":          
          {
           "email":"dskfhsiugfs@thing.com", 
           "password":"11111111",
           "password_confirmation":"111111112"}
           },
         "headers": 
           {
             "X-HTTP-Method-Override":"POST",
             "Accept":"application/json, text/plain, */*", 
             "Content-Type":"application/json;charset=utf-8",
             "X-XSRF-TOKEN":"kxvlsnonsdofsdkfjbsdif"}
           },
        "statusText":"Unprocessable Entity"
       }
我怎样才能只显示必要的东西,比如-电子邮件已被接收,密码不匹配

通过添加$scope.error=RESPONSE\u error.data.errors将其修复;如果有人需要

            Auth.register(credentials, config).then( 
              function(RESPONSE_SUCCESS) {
                console.log(RESPONSE_SUCCESS); // => {id: 1, ect: '...'}
                $scope.something = RESPONSE_SUCCESS; **//This is how you get it.**
            }, 
              function(RESPONSE_ERROR) {
                //This will only trigger if your server send an error back.
               $scope.error = RESPONSE_ERROR.data.errors;  //This will do the trick
            });

分配$scope.something以响应_SUCCESS之后,可以在视图中将其作为{{something}使用。看一看,

为什么你认为有错误?。我没有收到任何错误,它的工作方式也没有达到预期……正如你所看到的,我也收到了一个带有预期错误的json响应,但我不知道如何在Controllera中显示它。你收到响应了吗?我以为您只收到了响应。\u错误。是的,我获得了成功。我收到了一个带有错误的users.json文件。@ABHILASHV.R我已更新了我的答案。这是你想要的还是我还不明白?似乎是这样……我现在就确认。