I have a Advanced Custom Field on WordPress edit post admin page that requires some validation. This validation is done via ajax (I am pretty sure though that acf and WordPress are irrelevant to this question).
Unfortunately I get the following error in the console: Uncaught Not a valid flickr.com url
(line 16) for the following code:
jQuery(document).ready(function($) {
var initialVal = $('#acf-field_560aabcf8dbef').val();
$('#acf-field_560aabcf8dbef').on('paste', function(e) {
var pasteData = e.originalEvent.clipboardData.getData('text');
if (initialVal !== pasteData) {
try {
$.get(ajaxurl,
{
'action': 'ai_check_url_id',
'idOrUrl': pasteData
},
function(response) {
response = $.parseJSON(response);
if (response.error) {
throw response.error.msg;
}
console.log(response);
}
);
} catch (e) {
console.error(e);
}
};
});
});
The php function
<?php
function ajaxIdOrUrl()
{
try {
echo $this->idOrUrl($_GET['idOrUrl']);
} catch (Exception $e) {
echo json_encode([
'error' => [
'msg' => $e->getMessage(),
'code' => $e->getCode(),
],
]);
}
wp_die();
}
What it does is:
- the
ajaxurl
is defined on the page - on a paste event check if the value has changed
- if it has, make a
get
request and send the changed data along - the answer is either an ID or an error array
Can anybody point me in the right direction why this shows up as being uncaught (the code works fine when there is no error)?
Thanks to the input of Matt Gibson and this answer. I have reworked my jQuery to this: