javascript - 无法通过 Shopware 6 中的 XmlHttpRequest 请求 PageController

标签 javascript ajax symfony shopware symfony-routing

我正在尝试向我的自定义 StorefrontController 发送请求在最新的 Shopware 6.2.2但我收到以下错误:

PageController can't be requested via XmlHttpRequest.
我正在做一个常规的httpClient来自自定义 JS 插件的请求:

export default class MyCustomPlugin extends Plugin {

  static options = {
      dataUrl: '', // comes from the twig as "path('frontend.path.to.route')"
      product: null,
      params: {},
      loadingIndicatorClass: 'is-loading',
      responseSelector: 'some-selector-class'

  }

  init () {
      // this.el.innerHTML = LoadingIndicator.getTemplate()
      this.httpClient = new HttpClient()
      const query = querystring.stringify(this.options.product)
      this.sendDataRequest(query)
  }

  /**
   * Add classes to add loading styling.
   * Prevents the user from clicking filter labels during filter request.
   */
  addLoadingIndicatorClass () {
      this.el.classList.add(this.options.loadingIndicatorClass)
  }

  /**
   * Remove loading styling classes.
   */
  removeLoadingIndicatorClass () {
      this.el.classList.remove(this.options.loadingIndicatorClass)
  }

  /**
   * Send request to get filtered product data.
   *
   * @param {String} filterParams - active filters as querystring
   */
  sendDataRequest (filterParams) {

      this.addLoadingIndicatorClass()

      this.httpClient.abort()
      this.httpClient.get(`${this.options.dataUrl}?${filterParams}`, (response) => {
          this.renderResponse(response)
          this.removeLoadingIndicatorClass()
      })
  }

  /**
   * Inject the HTML of the response to the element.
   *
   * @param {String} response - HTML response
   */
  renderResponse (response) {
      ElementReplaceHelper.replaceFromMarkup(response, this.options.responseSelector, false)
      window.PluginManager.initializePlugins()
  }
}
这是我的StorefrontController路线:
    /**
     * @Route("/path/to_route", name="frontend.path.to.route", methods={"GET"})
     */
    public function someAction(Request $request, Context $context): JsonResponse
谁能告诉我为什么请求不去?我想在 Shopware 6 中向我自己的 Controller 发送一个简单的 AJAX 请求。
谢谢!

最佳答案

尝试添加:defaults={"XmlHttpRequest"=true}到您的路线。
因此,更改后您将拥有:

/**
* @Route("/path/to_route", name="frontend.path.to.route", methods={"GET"}, defaults={"XmlHttpRequest"=true})
*/
还要确保使用 @RouteScope 在 Controller 中正确定义范围。注解。由于您的代码是一个店面 JavaScript 插件,它应该有 storefront范围定义。

关于javascript - 无法通过 Shopware 6 中的 XmlHttpRequest 请求 PageController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62557435/

相关文章:

javascript - window.onload 函数问题

javascript - Highcharts 可拖动的故障工具提示

ASP.NET - 将 JSON 从 jQuery 传递到 ASHX

Symfony 4 应用程序与 Docker Compose 一起工作,但与 Docker Swarm 一起中断(无法登录,分析器中断)

php - $this->forward 丢失了用户的请求路由?

javascript - 如何将自定义 javascript 库包含到 google 网站?

javascript - 使用 python 脚本打印到 Chrome 控制台 - 如何调整以在 div 中打印?

java - 如何使用Extjs进行ajax调用以从jsp中的数据库获取数据?

ajax - Woocommerce Ajax 以编程方式添加到购物车

Symfony/FOSuserBundle : salt are null?