我正在使用formvalidation.io插件,并试图验证一个字段在数据库表中是唯一的。我已经做了比较,并且使用ajax返回结果(唯一的或不唯一的)。我使用表单验证插件(http://formvalidation.io/validators/callback)中的“回调验证器”。这是我的代码:
callback: { //check documento no repetido
message: 'Ya existe un estudiante con el mismo número de documento',
callback: function (value, validator, $field) {
var url = "documento-existe";
$.ajax({
type: "POST",
url: url,
data: $("#numero_documento").serialize(),
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
success: function(data)
{
console.log(data);
return data;
}
});
}
}
这段代码给出了javascript控制台中的错误ncaught TypeError: Cannot read property 'message' of undefined
。为什么会导致这个错误?
我知道表单验证插件中有一个“远程验证器”来执行ajax --通过验证(http://formvalidation.io/validators/remote/),但是我使用的是Laravel,我必须发送ajax头(X TOKEN),而“远程验证器”没有发送ajax头的能力。
发布于 2016-05-23 10:37:59
最后,“remote”方法接受标头。文件中没有具体说明这一点。我的解决办法是:
remote: {
message: 'Ya existe un estudiante con el mismo número de documento',
url: 'documento-existe',
type: 'POST',
data: function() {
return {
numero_documento: $("#numero_documento").val()
};
},
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
}
总之,我不喜欢控制台里的这个警告:
Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/.
https://stackoverflow.com/questions/37396444
复制相似问题