javascript - jQuery $.ajax 和 jQuery UI 对话框 : Uncaught TypeError: Illegal invocation?

标签 javascript jquery jquery-ui jquery-ui-dialog

当我单击 jQuery UI 对话框 Save 按钮时,我尝试发送 AJAX 请求,这就是我的做法:

$(function () {
    var comment_dlg = $('#add_comment_dialog');
    var quote_id = $('#comment_quote_id');

    $('#order_push').click(function () {
        quote_id.val($(this).data('id'));
        comment_dlg.dialog('open');
    });

    comment_dlg.dialog({
        title: "Write a comment",
        autoOpen: false,
        modal: true,
        width: 600,
        height: 300,
        buttons: {
            Cancel: function () {
                $(this).dialog('close');
            },
            'Save': function () {
                $.ajax({
                    url: Routing.generate('push_order_xml'),
                    method: 'GET',
                    data: { quote_id: quote_id },
                    cache: false
                }).done(function (data, textStatus, jqXHR) {
                    if (data.success.length) {
                        alert(data.success);
                    } else {
                        alert('Something went wrong!');
                    }
                });
            }
        }
    });
});

但我收到此错误:

Uncaught TypeError: Illegal invocation

不知道问题出在哪里。我已经多次检查了 jQuery UI Dialog 和 jQuery $.ajax 文档,我的代码似乎是正确的。

有什么想法吗?

最佳答案

好的,最后感谢 this answer我弄清楚问题出在哪里。

首先,因为这个:

var quote_id = $('#comment_quote_id')

quote_id 的值是整个 HTML,而不是我预期的值。

其次,我在这里为 $('#comment_quote_id') 赋值:

quote_id.val($(this).data('id'));

这是正确的。

第三,我的错误又是在这里使用quote_id:

data: { quote_id: quote_id }

这又是错误的,因为是整个 HTML 而不是值本身。

解决方案,使用quote_id.val() 例如:

data: { quote_id: quote_id.val() }

我无法使用 processData: false 因为我不想传递 HTML,而是传递值。

关于javascript - jQuery $.ajax 和 jQuery UI 对话框 : Uncaught TypeError: Illegal invocation?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44328555/

相关文章:

javascript - 如何获取刚刚放置在该 USED 上的可拖动对象要放入的 div 的 ID?

JavaScript 滚动到 jQuery 中绑定(bind)的 keydown 事件上的元素

javascript - swf 文件到 html-css-js

jquery - 鼠标悬停不会在生产环境中触发 jquery 动画

javascript - 在 Internet Explorer 上悬停表格标题时不显示工具提示

jquery - jquery ui 选项卡中取消选择选项卡上的事件

javascript - 帮助 jquery 选择器

javascript - 连字符不允许 jQuery AJAX 调用正常工作

使用 Django jquery ajax 文件上传

jquery Tagsinput 和 ui 自动完成 : can they work with pre-loaded source?