0

我正在使用 formvalidation.io 插件,我正在尝试验证一个字段在数据库表中是唯一的。我已经完成了比较,我正在使用 ajax 返回结果(是否唯一)。我使用来自 formvalidation 插件(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;
            }
        });
    }
}

ncaught TypeError: Cannot read property 'message' of undefined这段代码在 javascript 控制台中给了我一个错误。为什么会造成这个错误?

我知道有一个来自 formvalidation 插件的“远程验证器”来进行 ajax-through 验证(http://formvalidation.io/validators/remote/),但我使用的是 Laravel,我必须发送 ajax 标头(X- CSRF-TOKEN),并且“远程验证器”无法发送 ajax 标头。

4

1 回答 1

1

最后,“远程”方法接受标题。它没有在文档中指定。我的解决方案是:

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/.

于 2016-05-23T18:37:59.530 回答