Estoy tratando de enviar una solicitud a mi StorefrontController personalizado en el último Shopware 6.2.2 pero recibo el siguiente error:
PageController can't be requested via XmlHttpRequest. Estoy haciendo una solicitud httpClient regular como tal desde un complemento JS personalizado:
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() } } Y aquí está mi ruta StorefrontController :
/** * @Route("/path/to_route", name="frontend.path.to.route", methods={"GET"}) */ public function someAction(Request $request, Context $context): JsonResponse¿Alguien puede decirme por qué no va la solicitud? Quiero enviar una solicitud AJAX simple a mi propio controlador en Shopware 6.
¡Gracias!
Prueba con agregar:
defaults={"XmlHttpRequest"=true} a su ruta.
Así que después de los cambios tendrás:
/** * @Route("/path/to_route", name="frontend.path.to.route", methods={"GET"}, defaults={"XmlHttpRequest"=true}) */ También asegúrese de que el alcance esté correctamente definido en el controlador mediante la anotación @RouteScope . Dado que su código es un complemento de JavaScript de escaparate, debe tener definido el alcance del storefront .