gpt4 book ai didi

javascript - 未找到 Angular 异步验证器对象

转载 作者:行者123 更新时间:2023-12-03 02:56:40 26 4
gpt4 key购买 nike

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

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

POST http://127.0.0.1:8000/nameCheck[object%20Object] 404 (Not Found)

路线

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>

最佳答案

问题出在您的 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 调用抛出错误,您必须在回调中捕获它。

关于javascript - 未找到 Angular 异步验证器对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47595590/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com