gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 17:27:03 24 4
gpt4 key购买 nike

我正在尝试向我的自定义 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com