AJAX在OpenCart 2中调用PHP文件
我想调用我创建的系统/助手的PHP文件,该文件要求使用模型的方法,该模型在数据库中添加优惠券。 PHP文件包含以下内容。
<?php
function coupon_for_acumba() {
$this->load->model('total/coupon');
echo $this->model_total_coupon->coupon_test();
}
我创建了一个JS文件,该文件在提交表单时进行AJAX调用。文件中的脚本是以下内容
let acumbaForm = document.querySelector('#form-acm_28955');
acumbaForm.addEventListener('submit', function () {
setTimeout(function () {if (document.querySelector('.succes-alert-form-acm')) {
$.ajax({
url : '/system/helper/acumba.php',
type : 'POST',
success : function (result) {
console.log (result); // Here, you need to use response by PHP file.
},
error : function () {
console.log ('error');
}
});
}}, 2000)
})
,最后我用$ this-&gt; document-&gt; addscript('catalog/view/view/javascript/test1.js');
问题是,每次我提交表格时,我都会从Ajax调用中收到错误消息。你能告诉我我做错了什么吗?
I want to ajax call a php file which i created system/helper which calls for a method of a model which adds a coupon in the database. the php file contains the following.
<?php
function coupon_for_acumba() {
$this->load->model('total/coupon');
echo $this->model_total_coupon->coupon_test();
}
I created a js file which makes the ajax call when a form is submitted. The script in the file is the following
let acumbaForm = document.querySelector('#form-acm_28955');
acumbaForm.addEventListener('submit', function () {
setTimeout(function () {if (document.querySelector('.succes-alert-form-acm')) {
$.ajax({
url : '/system/helper/acumba.php',
type : 'POST',
success : function (result) {
console.log (result); // Here, you need to use response by PHP file.
},
error : function () {
console.log ('error');
}
});
}}, 2000)
})
finally i called this js file in catalog/controller/common/header.php with $this->document->addScript('catalog/view/javascript/test1.js');
The problem is that everytime i submit the form i get an error message from the ajax call. Can you tell me what i am doing wrong please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
OpenCart不允许直接从System文件夹拨打PHP文件(在System Folder中查看.htaccess文件)。尝试打开https:// yoursite/system/helper/acumba.php,您将被禁止403。您必须使用路由来调用方法。
您必须修改/catalog/controller/extension/total/coupon.php,然后使用您的方法,然后在JS文件中使用此方式。
OpenCart doesn't allow call PHP files directly from system folder (check .htaccess file in system folder). Try to open https://yoursite/system/helper/acumba.php, You'll get 403 Forbidden. You have to use a route to call a method.
You have to modify /catalog/controller/extension/total/coupon.php and put in your method, then call this way in your JS file.