Prestashop - 向 CategoryController 发出 AJAX 请求的正确方法
Prestashop - Right way to make AJAX request to CategoryController
我需要调用 AJAX 来覆盖前端控制器。我试过 returns 一个 403
状态代码的最后一种方法,但在 responseText
中它显示了正确的 JSON。
我如何调和并修复它以便正确 returns 一个 200 OK
以及正确的 JSON?
<?php
class CategoryController extends CategoryControllerCore{
public function init()
{
parent::init();
// check if URL contain ajax option and set it.
if( $this->ajax = Tools::getValue( "ajax" ) ){
if( $this->ajax ){
$action = Tools::getValue( 'action' );
if( !empty( $action ) && method_exists( $this, 'ajaxProcess' . Tools::toCamelCase( $action ) ) ){
// Return the method call
$this->{'ajaxProcess' . Tools::toCamelCase( $action )}();
} else {
$this->AjaxProcess();
}
// Required to avoid errorHandler in AJAX
exit;
}
}
}
public function ajaxProcessMyAjaxMethod()
{
$customerId = Tools::getValue( 'customerId' );
print Tools::jsonEncode( array(
'customerId' => $customerId,
'msg' => 'Hello PS AJAX!!!',
) );
}
}
这是我的 JavaScript 代码:
$.ajax({
url: baseUri + 'index.php',
type: 'GET',
cache: true,
dataType: 'json',
data: {
controller: 'Category',
action: 'MyAjaxMethod',
customerId: 45,
ajax: 1,
token: token
},
})
.done(function (data, textStatus, jqXHR) {
console.log(data);
})
.fail(function (jqXHR, textStatus, errorThrown) {
console.log(textStatus);
console.log(jqXHR.responseText);
});
CategoryController
必须成功获得 id_category
到 运行。您可以在此处添加它:
...
data: {
controller: 'Category', /* better lowercase 'category' */
id_category: 3, /* your ID category */
action: 'MyAjaxMethod', /* if you are using 'toCamelCase', it can be 'my_ajax_method' */
customerId: 45,
ajax: 1,
token: token
}
...
请确保将其传递到右侧 url(因此 url: baseUri + 'index.php'
是正确的)。
我需要调用 AJAX 来覆盖前端控制器。我试过 returns 一个 403
状态代码的最后一种方法,但在 responseText
中它显示了正确的 JSON。
我如何调和并修复它以便正确 returns 一个 200 OK
以及正确的 JSON?
<?php
class CategoryController extends CategoryControllerCore{
public function init()
{
parent::init();
// check if URL contain ajax option and set it.
if( $this->ajax = Tools::getValue( "ajax" ) ){
if( $this->ajax ){
$action = Tools::getValue( 'action' );
if( !empty( $action ) && method_exists( $this, 'ajaxProcess' . Tools::toCamelCase( $action ) ) ){
// Return the method call
$this->{'ajaxProcess' . Tools::toCamelCase( $action )}();
} else {
$this->AjaxProcess();
}
// Required to avoid errorHandler in AJAX
exit;
}
}
}
public function ajaxProcessMyAjaxMethod()
{
$customerId = Tools::getValue( 'customerId' );
print Tools::jsonEncode( array(
'customerId' => $customerId,
'msg' => 'Hello PS AJAX!!!',
) );
}
}
这是我的 JavaScript 代码:
$.ajax({
url: baseUri + 'index.php',
type: 'GET',
cache: true,
dataType: 'json',
data: {
controller: 'Category',
action: 'MyAjaxMethod',
customerId: 45,
ajax: 1,
token: token
},
})
.done(function (data, textStatus, jqXHR) {
console.log(data);
})
.fail(function (jqXHR, textStatus, errorThrown) {
console.log(textStatus);
console.log(jqXHR.responseText);
});
CategoryController
必须成功获得 id_category
到 运行。您可以在此处添加它:
...
data: {
controller: 'Category', /* better lowercase 'category' */
id_category: 3, /* your ID category */
action: 'MyAjaxMethod', /* if you are using 'toCamelCase', it can be 'my_ajax_method' */
customerId: 45,
ajax: 1,
token: token
}
...
请确保将其传递到右侧 url(因此 url: baseUri + 'index.php'
是正确的)。