我正在尝试在 elgg 中进行 AJAX 调用,但到目前为止我还没有成功。我可能做错了什么,或者我错过了什么?
提前谢谢大家。
Elgg版本2.1.1
start.php
// in myplugin_init()
elgg_register_ajax_view('forms/myplugin/add');
默认/表单/myplugin/add.php
<div>Successful AJAX call</div>
默认/对象/my_ajax_plugin.php
<div class="myplugin-form-container">JQ Here</div>
<script type = "text/javascript" language = "javascript">
var Ajax = require('elgg/Ajax');
var ajax = new Ajax();
ajax.form('myplugin/add').done(function (output, statusText, jqXHR) {
if (jqXHR.AjaxData.status == -1) {
return;
}
$('.myplugin-form-container').html(output);
});
</script>
Elgg 使用 requirejs 来加载依赖项。你不能仅仅抛出 require inline 并期望它能够工作。你可以尝试:
require(['elgg/Ajax'], Ajax => {
var ajax = new Ajax();
ajax.view('developers/ajax_demo.html').then(body => {
console.log('RESULT', body)
})
})
供参考:可以通过将“ajax.view”调用包装到事件侦听器中来将此操作附加到事件:
// Bind a unique event to this element
document.getElementById('trigger-unique-id').addEventListener('click', function() {
event.preventDefault(); // avoid submitting in case it is embedded into a form
[ ajax.view(); ]
}