当我单击 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/