0

我正在十月 CMS 中制作一个网站。

我创建了 1 个组件,比如说 Component_1 和 Component_2

现在,我想通过表单将数据从 Component_1 发布到 Component_2 [通过 AJAX 数据属性 APi]

因此,在我的 component_1 中,我有一个表单:

<form method="POST" data-request="Component_2::onSubmit" data-request-flash>

在 Component_2 我有方法 onSubmit(){ //Nothing Here }

当我提交表单时出现错误:

AJAX handler 'Component_2::onSubmit' was not found.

然后我在 Component_1::onSubmit 中具有相同的功能,当我将此表单提交给 Component_1::onSubmit 时,我没有收到任何错误。

那么,您能否建议我一种通过 AJAX 将表单从 component_1 提交到 component_2 的方法

谢谢

4

1 回答 1

1

嗯,它应该可以工作,请确保您使用的是component alias名称而不是使用它的class name,

只是为了通知我们not passing data from one component to another只是一个普通的ajax request,我们从browser to component

如果我声明class class componentOne { ... etc我们不会使用这个。

然后当we include它在页面内时,alias name您需要使用它alias name来请求 ajax。

在此处输入图像描述

更新(使用不同页面的 ajax 处理程序)

我们需要使用ajax JavaScript API它会给我们更多的灵活性

你可以使用这个代码

<!-- current page is `test` 
we are requesting ajax request on anohter `testing` page which is having 
compoTwo component and having `onAjaxReq` ajax handler -->
<button onClick="requestAnotherPage(this)" type="button" class="btn btn-danger" >
    My button
</button>

<script>
function requestAnotherPage(th) {
    $(th).request('compoTwo::onAjaxReq', {
        url: "{{ 'testing'|page }}", // <- this option
        // url: "http://timeloger.test/testing", 
        flash: 1,
        data: { id: 2, name: 'hardik'},     
        handleFlashMessage: function(message, type) {
            $.oc.flashMsg({ text: message, class: type })
        },
        success: function(data) { console.log('success'); this.success(data); }
    });
}
</script>

你需要照顾另一个页面的网址

{{ 'testing'|page }}将生成http://timeloger.test/testing您需要page File Name作为参数传递的该页面的 url {{ 'page-name'|page }}-> 它将生成正确的 url

现在ajax api可以向another given page url它提出请求fetch data

于 2018-01-22T16:39:47.500 回答