找不到角度异步验证器对象

时间:2017-12-01 14:30:58

标签: javascript angularjs laravel

我正在使用https://github.com/codekraft-studio/angular-async-validation

我收到此错误,我不确定为什么它没有成功传递数据。

  

POST http://127.0.0.1:8000/nameCheck[object%20Object] 404(未找到)

路线

Route::post('/nameCheck', 'UserController@checkName');

这是服务器端,我正在使用laravel 5.5

public function checkName(Request $request)
{
    $name = $request['name'];

    $existing_name = User::where('name', '=', $name)->first();

    if($existing_name == NULL){
        return 'name does not exists';

    }
    else{
        return 'name  exist';
    }
}

JS

app.directive('nameValidator', function($http, $q){
    return{
        require: 'ngModel',
        link:function(scope, element, attrs, ngModel){
            ngModel.$asyncValidators.name = function(modelValue, viewValue){
                return $http.post('/nameCheck'+ {name:viewValue}).then(
                    function(response){
                        if(!response.data.validName){
                            return $q.reject(response.data.errorMessage);
                        }
                        return true;
                    }

                );

            };
        }
    };

});

HTML

 <div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
                            <label for="name" class="col-md-4 control-label">Name</label>

                            <div class="col-md-6">
                                <input id="name" type="text" ng-model="userForm.model.name" class="form-control" name="name" value="{{ old('name') }}" name-validator required autofocus>

                                    <span ng-if="userForm.name.$pending">
                                      Checking Username...
                                    </span>
                                @if ($errors->has('name'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('name') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

1 个答案:

答案 0 :(得分:1)

问题出在POST来电:

 $http.post('/nameCheck', { name: viewValue}).then(
                    function(response){
                        if(!response.data.validName){
                            return $q.reject(response.data.errorMessage);
                        }
                        return true;
                    }

                );

以下是错误的:

$http.post('/nameCheck'+ {name:viewValue}).then ..

另外,请尝试添加错误回调,如果POST调用抛出错误,则必须在回调中捕获它。