Unable to access HTTP response headers during Ext.ajax.request callback in ExtJS 4.2.1

When making an Ext.ajax.request, I am attempting to read the headers of the response that is coming. Here is the code snippet:

Ext.Ajax.request({ url: 'http://localhost:3000/v0.1/login' ,
    method: 'POST',
    scope:this,
    jsonData: {"_username":username,"_userpwd":password},
    success: function(responseObject){
        var headers = responseObject.getAllResponseHeaders();
        console.info(headers );
        Ext.destroy(Ext.ComponentQuery.query('#loginWindow'));
        this.application.getController('SiteViewController').showView();
    },
    failure: function(responseObject){
        alert(responseObject.status);
    }
    });

However, when I check the console, only one header is printed out:

Object {content-type: "application/json; charset=utf-8"} 

All the other headers are missing from the output, even though they are visible in the Chrome inspector. What could be causing this discrepancy? Any insights would be appreciated. Thanks.

Answer №1

If you're making a cross-domain request, keep in mind that only the server can explicitly expose headers. Requests on the same domain will expose all headers without issue.

To ensure specific headers are exposed, add the "Access-Control-Expose-Headers" header on the server side with a list of headers separated by commas. In PHP, this would be done as follows:

header("Access-Control-Expose-Headers: Content-length, X-My-Own-Header");

These headers can then be accessed using methods like

responseObject.getAllResponseHeaders()
or
responseObject.getResponseHeader('content-type')
.

For more details on cross-domain requests and headers, check out this resource: http://www.html5rocks.com/en/tutorials/cors/

PS: Ace.Yin provided the correct answer, but unfortunately I lack the reputation to leave a simple comment.

Answer №2

After encountering the same issue, I stumbled upon a helpful solution at this link.

Here is a snippet regarding the headers:

Access-Control-Expose-Headers (optional) - 
The XMLHttpRequest 2 object provides a method called getResponseHeader() to retrieve specific response header values.
During a CORS request, this method can only access simple response headers. 
Simple response headers include:

- Cache-Control
- Content-Language
- Content-Type
- Expires
- Last-Modified
- Pragma

To allow clients to access other headers, you must use the Access-Control-Expose-Headers header. This header's value should be a comma-separated list of response headers that you wish to expose to the client.

I have not yet verified it, but it seems to be on the right path :)

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

Determine if an element is being hovered over using jQuery

How do I determine if the cursor is hovering over an element using jQuery or JS? I attempted to use $('#id').is(':hover'), but it doesn't seem to be functioning as expected. It's worth mentioning that I am calling this line ...

The continuous updating of images through AJAX results in an irritating flickering and blinking effect

Recently, I tackled the task of implementing a comment system for my blogging CMS project. My aim was simple - to fetch comments every second from a separate PHP file (e.g., comment.php) that retrieves data from a MySQL table and display it within a specif ...

The Chrome file storage system saves information in files

My challenge is to save data obtained from the Chrome geolocation API into a text file using the Chrome fileSystem API. However, despite successfully creating a file, it remains empty after the process. To simplify, I attempted to add the string '1234 ...

Developing a perpetually scrolling container within a webpage

I am attempting to implement a scrollable div on my webpage that can continuously load content. I am currently using the following code snippet for this --> http://jsfiddle.net/cyrus2013/Qq85d/ $(document).ready(function(){ function loadMoreContent() ...

Hide the background when the modal appears

I have successfully implemented a jQuery CustomBox modal on my website. However, I am facing an issue with hiding the content div behind the modal once it pops up. My goal is to make the content div reappear after the modal has been closed. You can view a ...

How can I assign an ID to retrieve the property of <?php $row->name;?> using AJAX?

As a newcomer to Code Igniter, I am working on implementing an edit button in front of each row. My goal is to have a pop-up or modal box appear without refreshing the page after clicking the edit button, displaying all the fields within that box. < ...

Obtain specific information from custom fields on the product page

What am I trying to achieve? I want to create a single product page that displays custom fields which need to be filled out before the item can be added to the cart. Additionally, I have implemented a button that should take all the values entered in the ...

Utilizing various if conditions in conjunction with the jQuery chart plugin Piety

Check out this Fiddle example I'm interested in experimenting with different color combinations using a small chart plugin called piety. The documentation demonstrates how to set different colors for a pie chart: $(".bar-colours-1").peity("bar", { ...

Jquery UI slider malfunctioning would require troubleshooting

I am currently working on implementing slider functionality with jQuery. The slider is visible and functioning properly in terms of taking in values, however, the values are not displayed on the screen as expected. I have used console.log to track the ui.v ...

Steps for including a collection of objects in FormData when transmitting data to a Spring server using AJAX

There seems to be an issue with the objs value being null in the code provided below. Despite trying different solutions, objs remains null. Can anyone advise on how to add a list of objects in FormData? When examining the payload on the Network tab, the ...

Differences between throwing errors, returning error objects, and using callbacks in Javascript

Currently, I am developing an assembler and simulator for a simplistic assembly language that my computer science students use during their classes. The project is being written in JavaScript with the intention of creating a user-friendly interface in the ...

Updating the value on button click using Rails 5, Ajax, and jQuery

Whenever the user clicks on the "Add to Cart" button, I want to update the number in my navbar div to display how many products are currently in their cart. To achieve this, I have set the remote: true attribute on the button. The navbar contains a div w ...

Tips for correcting the `/Date(xxxxxxxxxxxxx)/` formatting issue in asp.net mvc

As a programming novice, I am trying to display data from my database server on the web using a datatable in asp.net mvc. After following a tutorial video on YouTube, I encountered an issue where the date and time columns in my table are displaying as /Dat ...

Is it possible for me to set a timer on the 'keyup' event in order to decrease the frequency of updates?

The code I currently have is functional: $wmdInput.on('keyup', function () { var rawContent = $wmdInput.val(); scope.$apply(function () { ngModel.$setViewValue(rawContent); }); }); Unfortunately, it appears to slow down my t ...

How can I retrieve the line number of a code during runtime in JavaScript?

Is there a way to add a console.log statement that would indicate the line number it is on in JavaScript? For example: console.log ('Line:', [code to get the line]). The output in the console would be Line: [line number], helping me identify wher ...

"Modifying a variable within an object while iterating through a loop

I am attempting to update a variable within an object while looping through the elements. Specifically, I am targeting all parent elements with the class "parent" to use with the ScrollMagic plugin. Here is my code: var childElement = $('.child' ...

Using AngularJS in conjunction with other scripts

I am currently working on an application and now I have the task of implementing a dynamic menu using AngularJS. This requires me to modify variables in the AngularJS application from my existing code. Here is the example I am experimenting with: <scr ...

I keep running into an issue whenever I attempt to import material ui icons and core - a frustrating error message pops up stating that the module cannot be found

[I keep encountering this error message when attempting to utilize @material-ui/core and icons] `import React from "react"; import "./Sidebar.CSS"; import SearchIcon from "@material-ui/icons/Search"; const Sidebar = () => { return ( <> ...

The application suddenly displays a blank white screen after encapsulating my layout with a context provider

Here is the custom layout I created: export const metadata = { title: "Web App", description: "First Project in Next.js", }; export default function CustomLayout({ children }) { return ( <html lang="en"> ...

Utilizing Ajax in conjunction with the func_get_args() function

Can php's func_get_args() be used to capture 'post(ed)' data from an Ajax function call? (Currently using json to post data). Example of a hypothetical Ajax call posting data: . url: '.../.../...?action=function' data: { someStri ...