使用UUID时Ajax调用出错

Error in Ajax call when using UUID

本文关键字:调用 出错 Ajax UUID 使用      更新时间:2023-09-26

在单击链接时,我调用了一个进行Ajax调用的方法。问题是,当我在第一个参数中使用UUID字符串时,它会抛出一个错误:

Uncaught TypeError: Illegal invocation

下面是一个链接的例子:

onclick="fetchQuestion(ff8080814e6c440b014e6c464f660001,2);return false"

但是当我在第一个参数中使用简单ID时,它工作得很好。例子:

onclick="fetchQuestion(3,2);return false"
以下是fetchQuestion方法的实现:
function fetchQuestion(questionId, index){
    $.ajax({
        url: '${createLink(action: 'fetchQuestion')}',
        data: {
            testAttemptedId: ${testAttemptedId},
            index: index,
            id: questionId,
            grantAssessmentId: ${grantAssessmentId}
        },
        cache: false,
        success: function(html) {
            $('div[id="questionContent"]').html(html);
        }
    });
}

为什么它不能在UUID的情况下工作?

由于UUID为string,因此需要将UUID用引号括起来。

onclick="fetchQuestion('ff8080814e6c440b014e6c464f660001', 2);return false"
//                     ^                                ^

您需要将值放在引号中,以便将其识别为字符串。

onclick="fetchQuestion('ff8080814e6c440b014e6c464f660001',2);return false"

请注意,您应该使用Javascript来附加事件,因为它允许更好地分离关注点。试试这个:

<a href="#" data-uuid="ff8080814e6c440b014e6c464f660001" daat-index="2">Foo</a>

然后你可以在JS中钩到它:

$(function() {
    $('a').click(function(e) {
        e.preventDefault();
        fetchQuestion($(this).data('uuid'), $(this).data('index'));
    });
});