我有一个问题,让我封锁,我需要帮助!
通过使用knockoutJs和一个控制器来获得一个form_key,我试图重定向到wishlist,以便根据url添加产品:wishlist/index/add
我试着用以下方法来做这件事:在击倒J中:
addToWishlist(productRelatedId) {
console.log("product id", productRelatedId);
const params = {
'productId': productRelatedId,
};
var url = urlBuilder.build('related/wishlist/data');
/*var url = '/test_route/index/test';
var param = {eventid: attribute1.value};*/
$.ajax({
url: url,
data: params,
type: "GET",
success : function (response) {
console.log(response);
}
});
}
在控制器中:
<?php
namespace Devlat\RelatedProducts\Controller\Wishlist;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Data\Form\FormKey;
class Data implements HttpGetActionInterface
{
/**
* @var FormKey
*/
private $formKey;
/**
* @var ResultFactory
*/
private $resultFactory;
/**
* @var RequestInterface
*/
private $request;
public function __construct(
FormKey $formKey,
ResultFactory $resultFactory,
RequestInterface $request
)
{
$this->formKey = $formKey;
$this->resultFactory = $resultFactory;
$this->request = $request;
}
public function execute()
{
$formKey = $this->formKey->getFormKey();
$data = $this->request->getParams();
$params = [
'form_key' => $formKey,
'product' => $data['productId'],
];
$redirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
$redirect->setUrl('wishlist/index/add', $params);
return $redirect;
}`
我收到了预期的产品ID,但这内弗斯重定向我,我做错了什么?还是我错过了什么?
谢谢!我很感激你的帮助!
1条答案
按热度按时间u0njafvf1#
你试过这样的东西吗?