如何进行 AJAX 调用 - Elgg

问题描述 投票:0回答:2

我正在尝试在 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>
javascript php html ajax elgg
2个回答
2
投票

Elgg 使用 requirejs 来加载依赖项。你不能仅仅抛出 require inline 并期望它能够工作。你可以尝试:

require(['elgg/Ajax'], Ajax => {
  var ajax = new Ajax();
  ajax.view('developers/ajax_demo.html').then(body => {
    console.log('RESULT', body)
  })
})

0
投票

供参考:可以通过将“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(); ]
}
© www.soinside.com 2019 - 2024. All rights reserved.