XMLHttpRequest : Problem with "setRequestHeader" in Firefox

I need to customize request headers for my ajax POST calls, but I'm facing some challenges due to the framework I am using (ZK). I can only set the request headers by overriding certain functions. This is what I have done:

var zk_native_function_to_instanciate_a_xhr_OLD = zk_native_function_to_instanciate_a_xhr();
zk_native_function_to_instanciate_a_xhr = function() {
    var xhr = zk_native_function_to_instanciate_a_xhr_OLD ();
    xhr.onloadstart = function () {
      if (xhr.readyState === 1) {
        xhr.setRequestHeader("XX-MY-CUSTOM-HEADER", "foobarhimself");
      }
    }
};

This setup works fine in Chrome, but it doesn't work as expected in Firefox. When debugging in Firefox, I noticed that the ready state changes too quickly from 1 to 4 when reaching the 'if' line compared to Chrome.

Even though both browsers return an instance of XMLHttpRequest object from

zk_native_function_to_create_a_xhr_OLD()
, there seems to be a discrepancy in how they handle setting request headers asynchronously.

When trying to remove the breakpoint on Firebug, I encountered an error message saying 'InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable'. It appears that this is happening because the readyState is already at 4 by the time xhr.setRequestHeader() is called.

Can anyone suggest a solution to this issue? Is there another way to call xhr.setRequestHeader() or modify the event handlers?

Thank you for your help!

Answer №1

After much effort, I managed to find a solution to this issue by directly overriding the native open() function that is utilized within the embedded framework.

let old_zk_native_function = zk_native_function_to_instanciate_a_xhr();
zk_native_function_to_instanciate_a_xhr = function() {
  let xhr = old_zk_native_function();
  let xhr_open_base = xhr.open;
  
  xhr.open = function (method, url, async, user, password) {
    xhr_open_base.apply(this, arguments);
    xhr.setRequestHeader("XX-MY-CUSTOM-HEADER", "foobarhimself");
  }
};

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

The update function for colors in Three.js is not being triggered when colorsNeed

I'm attempting to change the color of a face on an external event - button click. Despite searching for solutions, none have been successful so far. I have an interactive model that I want to be able to toggle its colors. The model is rendered and co ...

Syntax error encountered while parsing JSON data (jQuery version 1.7.2)

My current challenge involves extracting data from a JSON object located on a different page within my website. This particular page is hosted on an ecommerce platform, which limits my access to server side controls and certain elements. I have encountere ...

Create data according to jQuery's AJAX data method

When editing in place using jQuery, if the code snippet seems too complicated, please refer to a simpler one. 'column2' => "RAJANgsfdgf" 'column3' => "SRARDHAMgsdfgdf" 'column4' => "40043433" 'column7' =&g ...

Utilizing jQuery for AJAX-Based Deletion of Records

Hey, I stumbled upon a code snippet that allows me to delete a row from my database using Ajax. However, there seems to be an issue where the table that should disappear when deleted is not working properly. Here is the JavaScript code: <script type=" ...

The select options appear above the overlay modal

In my HTML application on Apex 5, I have implemented a drop-down list that is not meant to be directly selected by the user. Instead, when the user clicks on it (or when the item receives focus), a modal page opens displaying the list of items for selectio ...

Shadows on menu buttons transform when clicked using React and CSS

I'm currently working on customizing the styling of a menu using CSS in a project that involves the use of "react-horizontal-scrolling-menu". While I've been successful in styling the menu items with .menu-item:hover & .menu-item:active, I am ...

Animating text with a shake effect using JQuery

I am attempting to create a shake effect on my text input field when validation fails. While I have achieved the shake effect, I am unsure how to customize the default values for direction, distance, and times. Additionally, I believe there is an error i ...

Bootstrap UI Tab presents an obstacle for Google Map functionality

When utilizing the Bootstrap Tabset to include a Bootstrap slider, Google Map, and other pages, I encountered an issue where the slider functions perfectly but the Google Map does not work as expected. Interestingly, the map works perfectly in street view ...

Obtain the YouTube video identifier from a YouTube embedded link

Is there a way to extract just the YouTube ID from a given URL? https://www.youtube.com/embed/cqyziA30whE?controls=1&showinfo=0&rel=0&autoplay=0&loop=0 $(".youtube").click(function () { console.log(this.href.replace(new RegExp("em ...

A guide on navigating full images using CSS

I am searching for a code that will enable scrolling through entire images on my landing page. It is a bit challenging to explain, but it is similar to the style of scrolling on tesla.com. Each scroll of the mouse wheel moves down one complete image. Can ...

Encountering difficulties in constructing next.js version 14.1.0

When attempting to build my next.js application, I use the command npm run build Upon running this command, I encountered several errorshttps://i.sstatic.net/5jezCKHO.png Do I need to address each warning individually or is there a way to bypass them? B ...

AJAX Post data transmission on the network: Enhancing data formatting

For my AJAX post call, I need to format the data differently. The server is expecting the data in a specific format when viewed in Chrome Network Headers details. My task is to update the JavaScript code below to meet this formatting requirement: percenta ...

How can Angular hide a global component when a particular route is accessed?

Is it possible to hide a global component in Angular when a specific route is opened? For instance, consider the following code: app.component.html <app-header></app-header> <app-banner></app-banner> <!-- Global Component I w ...

Outputting Javascript Strings via PHP

Currently, I am utilizing a web service that generates tables through JavaScript functions. However, I am in need of the table to be generated in plain HTML format instead. One potential solution to achieve this is by transferring the JavaScript string t ...

Select identifiable qualities on the user interface

Is there a way to display a list of searchable attributes alongside the search box? Perhaps giving users the option to select which attributes to search by. For instance, when searching for a movie, users could specify whether they want to search by title ...

Analyzing the path of the cursor

Looking to enhance my tracking capabilities by monitoring mouse movements. I have the ability to capture XY coordinates, but understand that they may vary depending on browser size. Are there any other parameters recommended for accurate results? P.S: Be ...

The text for the Google chart is nowhere to be found

After creating a test project using the Google Chart example, I noticed that some text from the chart is missing after adding CSS. Here is the CSS code I used: html, body { font-size: 100%; height: 100%; width: 100%; } body { background: ...

I keep encountering an issue with npm where it is unable to identify the executable to run whenever I launch VS Code and attempt to use Cypress

Whenever I open Visual Studio Code and try to run 'npx open cypress', I keep encountering this message in my terminal: npm ERR! could not determine executable to run Oddly enough, running 'npx cypress -v' provides version details, so ...

How can I personalize pagination with the reactable library?

As I work on implementing pagination for a reactable table, I have referred to the documentation which clearly outlines how to add this functionality using itemsPerPage and pageButtonLimit: <Table className="table" data={[ { Name: 'Griffin Smi ...

In what way can you dynamically set the displayName using a higher order component?

My current struggle is setting displayName dynamically in order to improve the debugging process. Each component renders as <Component ... />, making it difficult to identify them in the codebase. While I have successfully set static displayName on e ...