当前位置: 首页 > 工具软件 > Parsley > 使用案例 >

jquery parsley ajax,jquery - Parsley Custom Remote Validation with AJAX - Stack Overflow

燕玉堂
2023-12-01

The problem is that the AJAX call to your remote validator is being made asynchronously and that means that your response is coming back too late for Parsley, so it will always assume a false result for the validation.

While synchronous AJAX calls should be avoided, if you add the async: false option to your AJAX call your code should work:

window.ParsleyValidator.addValidator('cardcode',

function (value) {

var valid = false;

$.ajax({

url: '/data/checkout/cvvCheck.json',

data: {

cvv: value

},

async: false,

success: function(response) {

if(response.valid === true) {

return true;

} else {

return false;

}

}

});

}, 32);

Here's an asynchronous method that I found that worked for me - you'll need to load parsley.remote.min.js instead of parsley.min.js, then setup a custom remote validator using this code:

window.Parsley.addAsyncValidator('cardcode', function (xhr) {

var response = xhr.responseText;

if (response.valid === true) {

return true;

} else {

return false;

}

}, '/data/checkout/cvvCheck.json');

 类似资料: