简体   繁体   English

尝试在ajax调用中进行jQuery“未捕获”的jQuery ... catch

[英]jQuery “Uncaught” on ajax call inside try… catch

I have a Advanced Custom Field on WordPress edit post admin page that requires some validation. 我在WordPress编辑后管理页面上有一个高级自定义字段,需要进行一些验证。 This validation is done via ajax (I am pretty sure though that acf and WordPress are irrelevant to this question). 这个验证是通过ajax完成的(我很确定,尽管acf和WordPress与这个问题无关)。

Unfortunately I get the following error in the console: Uncaught Not a valid flickr.com url (line 16) for the following code: 不幸的是,我在控制台中收到以下错误: Uncaught Not a valid flickr.com url以下代码Uncaught Not a valid flickr.com url (第16行):

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功能

<?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 ajaxurl在页面上定义
  • on a paste event check if the value has changed 在粘贴事件中检查值是否已更改
  • if it has, make a get request and send the changed data along 如果有的话,发出一个get请求并发送更改的数据
  • the answer is either an ID or an error array 答案是ID或错误数组

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 . 感谢Matt Gibson的投入和这个答案 I have reworked my jQuery to this: 我已经将jQuery修改为:

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) {
            $.get(
                ajaxurl, 
                {
                    'action': 'ai_check_url_id',
                    'idOrUrl': idOrUrl
                }
            )
            .then(function(response) {
                response = $.parseJSON(response);
                if (response.error) {
                    return $.Deferred().reject(response.error)
                } else {
                    return response;
                }
            })
            .done(function(response) {
                console.log(response);
                // more sucess handling
            })
            .fail(function(err) {
                console.error(err.msg);
                // more error handling
            });
        };
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM