Accessing PageController through XmlHttpRequest is not allowed in Shopware 6

I'm encountering an issue when attempting to send a request to my customized StorefrontController on the most recent version of Shopware 6.2.2. The error message I'm receiving is:

PageController can't be requested via XmlHttpRequest.

I'm making a standard httpClient request from a custom JS plugin like this:


export default class MyCustomPlugin extends Plugin {

  static options = {
      dataUrl: '', 
      product: null,
      params: {},
      loadingIndicatorClass: 'is-loading',
      responseSelector: 'some-selector-class'

  }

  init () {
      this.httpClient = new HttpClient()
      const query = querystring.stringify(this.options.product)
      this.sendDataRequest(query)
  }

  addLoadingIndicatorClass () {
      this.el.classList.add(this.options.loadingIndicatorClass)
  }

  removeLoadingIndicatorClass () {
      this.el.classList.remove(this.options.loadingIndicatorClass)
  }

  sendDataRequest (filterParams) {
      this.addLoadingIndicatorClass()

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

  renderResponse (response) {
      ElementReplaceHelper.replaceFromMarkup(response, this.options.responseSelector, false)
      window.PluginManager.initializePlugins()
  }
}

Below is my StorefrontController route:


/**
     * @Route("/path/to_route", name="frontend.path.to.route", methods={"GET"})
     */
    public function someAction(Request $request, Context $context): JsonResponse

I'm puzzled as to why the request is not working. I simply want to send a basic AJAX request to my controller in Shopware 6. Any insights would be greatly appreciated.

Thank you!

Answer №1

Consider adding the following:

defaults={"XmlHttpRequest"=true}
to your Route.

After making these adjustments, your Route should look like this:

/**
* @Route("/path/to_route", name="frontend.path.to.route", methods={"GET"}, defaults={"XmlHttpRequest"=true})
*/

Also, double-check that the scope is correctly defined in the controller using the @RouteScope annotation. Since your code is a frontend JavaScript plugin, it should have the frontend scope specified.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

json undefined result or results when viewing in MVC

After passing the LINQ result to my dropdown list, I am seeing "undefined" displayed. The issue could be because I didn't specify a specific class after "select new," although that is how I need it. var newlist_of_device_types = (from z in DB.t_d ...

I am encountering a problem with the setState function, and receiving the error message: "Cannot read property 'setState' of null"

I need assistance with my code. There seems to be an issue, possibly a syntax error, but I am having trouble pinpointing it: import React, { Component } from 'react'; class Note extends Component { constructor(props) { super(props); t ...

Update the color of navigation items to reflect their active status

Here is the snippet of HTML code: <header> <nav> <a href="#" id="menu-icon"></a> <ul> <li><a href="#"">Home</a></li> <li><a href="#">About</a></li> & ...

Obtaining the source code in CKEditor while in edit mode with Rails

As a Rails developer, I recently utilized CKEditor in one of my applications. After writing a sample HTML source code in the editor and submitting it, the code displayed properly on the front-end as a GUI. However, when attempting to edit the source code f ...

What is the method for implementing type notation with `React.useState`?

Currently working with React 16.8.3 and hooks, I am trying to implement React.useState type Mode = 'confirm' | 'deny' type Option = Number | null const [mode, setMode] = React.useState('confirm') const [option, setOption] ...

"Troubleshooting JSON parsing issues with simple_form and AJAX integration in a Ruby on

Currently, there is a form on the users dashboard that allows them to add a custom game rule. This form utilizes Simple Forms and remote => true, loaded via Jquery's .load() function from the "rules" view, which is managed by the rules controller. ...

Tips for retrieving values from numerous checkboxes sharing the same class using jQuery

Currently, I am struggling with getting the values of all checkboxes that are checked using jquery. My goal is to store these values in an array, but I am encountering difficulties. Can anyone provide me with guidance on how to achieve this? Below is what ...

Achieving the utilization of Ajax for two users at the same

When making an Ajax request, I need to retrieve information for user one and user two. Do I have to perform separate queries like this? $check = "SELECT * FROM user WHERE id='$user1_id'"; $check1 = mysqli_query($mysqli,$check); $resultArr = mysq ...

A guide to extracting a Primitive Array from ES6 Promises

Currently, my goal is to load glsl scripts as strings by utilizing the ES6 Promise and Fetch APIs. In my attempt to achieve this, I believed that I had devised an elegant solution for fetching the vertex and fragment shaders and then generating a new progr ...

Using Vue.js to implement dynamic table rowspan functionality

Has anyone experimented with implementing dynamic table rowspan in vue.js? Here is the sample data { date: '2018-08-14', temp_que : 120, }, { date: '2018-08-14', temp_que : 120, }, { date: '2018-08-15', ...

Tips on reloading or refreshing a react-table component using my form component

I currently have two reactJS components set up: CustomerForm component, which includes a form along with form handling code. CustomerList component, which utilizes react-table to list the customers. Both components are fully functional and operational. ...

Guidelines for adjusting the next/image component to a full 100% height

In my Next.js application, I am trying to display an image that fills the full height of its container while automatically adjusting its width based on its aspect ratio. I attempted the following method: <Image src="/deco.svg" alt=&qu ...

Node.js: Promise chain abruptly stops after reaching a predefined limit without causing any errors

Currently, I am attempting to perform a straightforward operation in nodejs using promises. My task involves working with an array that consists of objects. These objects contain query parameters for a URL that I need to access through a GET request. As th ...

Generate a custom website using React to display multiple copies of a single item dynamically

As a newcomer to React and web development, I've been pondering the possibility of creating dynamic webpages. Let's say I have a .json file containing information about various soccer leagues, structured like this: "api": { "results": 1376, ...

What are the different scenarios in AngularJS where $scope is utilized versus when var is utilized?

Which is more efficient in AngularJS: using var or $scope. for variables inside functions? I am asking this question because I recently came across information about $watch, $digest, and $apply in AngularJS. While I may not have fully grasped the concept ...

"Encountering a problem with a missing object in jQuery when referencing

I'm encountering an issue with jQuery's $(this) object that's causing me to lose track of the this element in my code: $('.star').click(function (){ var id = $(this).parent().attr('id').split('rating')[ ...

Issues with accessing CakePHP routes using the HTTP Option request

I'm facing a challenge with implementing basic authentication in my Cake app. I've reached the stage where I need to send an ajax request with an authorization header containing my username and password encoded in base64. However, when this requ ...

There was an error in Threejs' PropertyBinding as it attempted to parse the trackName ".bones[].position

Version: THREE.WebGLRenderer 91dev Struggling to achieve a basic chest opening animation using three.js. Unfortunately, encountering an error while trying to create an action. PropertyBinding: Unable to interpret trackName: .bones[].position Link to t ...

Gathering information from various web pages simultaneously without the need to manually navigate through each page using a webdriver

I'm new to the world of web scraping and I've successfully developed a program that allows me to extract specific, dynamic data by utilizing the selenium web driver. My current project involves scraping data from a FAQ page in order to gather in ...

Leveraging external parameters in Symfony for assertions and routing

We are currently engaged in a major Symfony2 project with a focus on achieving maximum portability and configurability. Security is of paramount importance to us, leading us to implement extensive validation measures throughout the project. For instance, w ...