[英]403 Forbidden AJAX request in Magento 2
I am using Magento 2 i want to send an AJAX request from my custom js. 我正在使用Magento 2,我想从我的自定义js发送AJAX请求。
Here is the code 这是代码
jQuery.ajax({
method: "POST",
url: "app/code/Preview/Tag/Block/Baz/getTitle",
data: {data: doc.output()},
}).done(function(data){
console.log(data);
});
But it returns 403 forbidden. 但它返回403禁止。
Please let me know what is causing the issue. 请让我知道导致问题的原因。
Controller code: 控制器代码:
root_dir/app/code/Preview/Tag/Block/Baz.php root_dir / app /代码/Preview/Tag/Block/Baz.php
<?php
namespace Preview\Tag\Block;
/**
* Baz block
*/
class Baz extends \Magento\Framework\View\Element\Template
{
public function getTitle()
{
return "Foo Bar Baz";
}
}
?>
URL you are using is wrong , you need to learn about Magento2 Structure before you start code in Magento2. 您使用的URL错误,在Magento2中启动代码之前,您需要了解Magento2结构。
what you are using is a Block not a Controller, 您正在使用的是块而不是控制器,
1st you need to define a route in file etc/frontend/routes.xml 首先,您需要在文件etc / frontend / routes.xml中定义一条路由
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard">
<route id="previewtag" frontName="previewtag">
<module name="Preview_Tag" />
</route>
</router>
</config>
than after you need to define Controller file Controller/Index/Index.php 比之后您需要定义控制器文件Controller / Index / Index.php
<?php
namespace Preview\Tag\Controller\Index;
class Index extends \Magento\Framework\App\Action\Action
{
public function execute()
{
// your code here
echo "Foo Bar Baz";
}
}
now you can use the URL as 现在您可以将该网址用作
previewtag/index/index
or previewtag
previewtag/index/index
或previewtag
both works same 两者都一样
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.