Is it possible to retrieve the request URL from an XHR object? I noticed that the URL is visible in Firebug through the channel property, but unfortunately it cannot be queried using JavaScript.
Is it possible to retrieve the request URL from an XHR object? I noticed that the URL is visible in Firebug through the channel property, but unfortunately it cannot be queried using JavaScript.
Here is a clever trick that eliminates the need for a wrapper:
let xhrPrototype = XMLHttpRequest.prototype,
originalOpen = xhrPrototype.open;
xhrPrototype.open = function (method, url) {
this._url = url;
return originalOpen.apply(this, arguments);
};
Example of how to use it:
let request = new XMLHttpRequest();
request.open('GET', '...', true);
alert(request._url); // This will show an alert box with '...'
For those utilizing jQuery, the "beforeSend" function in your AJAX request can be a powerful tool for modifying the jqXHR object. Here's an example:
$.ajax({
...
url: "http://another/url",
beforeSend: function(jqxhr, settings) { jqxhr.newURL = "http://another/url"; },
...
});
By adding jqXHR.newURL
to the jqXHR object in this manner, you'll have access to it in any of the callback functions.
It seems like I grasp the issue you're facing.
Creating a wrapper for your XHR objects to enable specific functionality shouldn't be too complex.
Here is a basic example to illustrate:
// It is assumed that GetXHR() generates a new XHR object, compatible with different browsers.
function HTTPGet(url, onStartCb, onCompleteCb)
{
var xhr = GetXHR();
// Create a custom xhr object containing the URL and the actual xhr object.
var myXhr = { xhr: xhr, url: url };
xhr.onreadystatechange = function()
{
if (xhr.readyState == 4 && xhr.status == 200)
{
onCompleteCb(myXhr);
}
};
xhr.open("GET", url);
onStartCb(myXhr);
xhr.send(null);
}
This code hasn't been extensively tested, but it should function properly. With some adjustments (such as error handling, parameter passing, etc.), you can likely transform this example into a fully operational solution.
As per the information on https://developer.mozilla.org/en/XmlHttpRequest, gaining access to the channel
is possible with elevated privileges. However, it's important to note that the use of channel
is not standard and may not be supported in all browsers. The W3C specifications found at http://www.w3.org/TR/XMLHttpRequest/ do not make any reference to channel
or any method for "extracting the request URL". This leads me to believe that achieving this task consistently across different browsers may not be feasible.
Two useful properties that are worth considering include:
xhr2
XMLHttpRequest.responseURL
- https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseURL.
This feature is not compatible with Internet Explorer.
fetch
If you utilize fetch, you can access Response.url
- https://developer.mozilla.org/en-US/docs/Web/API/Response/url.
Please note that this functionality is not supported by Internet Explorer.
This solution came in handy for me as I was working on a for loop that involved making URL requests.
Typically, when making a URL request and then calling the open method on the XMLHttpRequest object, you can set a random property on the xml object (in my case, the property url):
var xml = new XMLHttpRequest();
xml.onreadystatechange = function() {
if ( xml.readyState == 4) {
console.log(xml.url); // same as originalUrl
}
};
xml.open("GET", originalUrl, true);
xml.url = originalUrl;
xml.send();
Currently, I am diving into the world of synchronous and asynchronous writing in node.js. With curiosity driving me, I decided to explore all active threads while experimenting with writing a random line to a txt file using fs.writeFile(). Any insights o ...
I'm a beginner in web development and I have some basic knowledge: HTML - structure of websites CSS - design aspect JavaScript - for adding interactivity Now, what exactly is jQuery, AngularJS, and Node.js? Upon researching, I discovered that jQue ...
Hey there, I'm currently working on an Image Gallery project. I have arranged thumbnails horizontally in a div below the main images. Take a look at this snapshot img. My goal is to have the thumbnails scroll along with the main pictures as the user ...
Currently, I am integrating webkitspeechRecongition into a form to allow users to enter data using their voice by pressing a button. However, a challenge arises when the call is triggered by a button within the form as it results in a post/submit action th ...
Embarking on my journey with VueJS. I previously implemented an alert box using jQuery. Now, I am attempting to recreate that in VueJS. Here is my current progress: 1- Developed a component named NovinAlert.vue which includes: <template> & ...
I'm currently using a code to analyze daily occurrences over the past 7 days. However, the code only captures days with occurrences and skips those without any data. I want to include all days in the analysis, even if they have a count of 0. Here&apos ...
Exploring the capabilities of the Beaker Browser API has led me to the need for making multiple (at least 20) ajax requests to extract content from another user's website. The essential arrays in play are: var posts = []; // Data from ajax requests ...
Dealing with a frustrating issue here. I've been working on an app and for the past few weeks, it keeps crashing unexpectedly. It seems to happen more frequently after saving my code to trigger a reload, but it can also just crash randomly while navig ...
Attempting to run a jest test, it seemed like the issue was with the expect, toBe section as I believed that the two objects being compared (data, geonames) were exactly the same. However, upon closer inspection, they turned out to be different. The struct ...
During my previous project, I utilized the open-source Foundation 4 framework and successfully implemented a top bar navigation. Now, as I embark on a new project with Foundation, I have downloaded the Foundation 4.3.2 version from . Despite referencing th ...
I'm trying to figure out how to access a method or variable that is defined in the Vuex 4 store within my Blade file. Currently, I am utilizing a compiled app.js from Vite. While I can easily access the store in Vue.js components, I am curious if it&a ...
I am currently implementing react-select in one of my projects. I need the wordWrap property to be set to "scroll". However, when the length of the options exceeds the menu width and I scroll to the right, the hover color does not fill the entire width. T ...
I am looking to create a simple button that, when clicked, redirects a user to a specific route that I have defined in my Index.tsx file. After clicking the button, the URL bar correctly changes to "/dashboard", but the component (just an h1) does not app ...
Below is the code I am using to ping a list of computers with Jquery and asp.net. function ping() { $('#progress').css("display", ""); $('.comp').each(function () { var $computer = $ ...
There are two images to choose from, and based on your selection using ng-click, the class changes. When moving on to the next step, a new object is created to store references to the selected image: $scope.cabinetDetails.cabinetType = { name: $scope. ...
The issue I'm facing involves determining the correct width for scrolling left or right. This results in the navigation tabs not scrolling correctly one by one. Each time I click the scroll right button, it adds more width than necessary and causes th ...
Can the issue of incorrect triangle activation be fixed when hovering on http://jsfiddle.net/2AXhR/? Sometimes the wrong triangle is triggered due to the triangles' bounding areas being rectangles, causing overlap and z-index conflicts. <style ...
I am currently working on a piece of code that retrieves the value from dropdown list items and then displays it in the document. To proceed, please select a fruit and click the button: <select id="mySelect"> <option>Apple</option ...
Is there a way to merge partial views using the templateUrl attribute in an AngularJS directive? Imagine having a directive like this: .directive('combinePartials', function () { var mainPartial = "mainpartial.html", var template2 = "pa ...
Is there a way to vertically align text over an image in react native? I came across this helpful guide, but I encountered difficulties trying to implement it. Specifically, I couldn't figure out how to nest the text tag within the Image tag. Below is ...