Capturing the request headers when making an HTTP call with AngularJS

Hey! I need some help with displaying the content of headers. Is there a simple way to do this? Here's my code snippet:

this.http.post(this.url, '{"username":"user","password":"123456"}')
.subscribe((res) => {
 var payload = res.json();
 var headers = res.headers;
 console.log('Content-Range: ' + headers.values());

However, I'm encountering an error:

ERROR Object { _body: error, status: 0, ok: false, statusText: "", headers: Object, type: 3, url: null }

==================================UPDATE================================ I am attempting to make a post request to a server, but it seems that Angular throws an error right after http.post due to an empty body. As such, I cannot perform a console log. I am only interested in the headers and not the body.

Answer №1

Attempt to accomplish it in this manner.

   access() {
     let data = new URLSearchParams;
    data.append('email',this.user);
    data.append('key',this.password);
        this.http.send(this.link,{header:header,search:data} )
            .map((reply) => reply.json())
                }
            }
        }

Answer №2

After some troubleshooting, I was able to resolve my issue by manually configuring the headers in my code. Here's the solution:

res.setHeader("Access-Control-Expose-Headers", "Origin, X-Requested-With, Content-Type, Accept,Authorization,Access-Control-Allow-Origin")
;

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

Understanding TypeScript typing when passing arguments to the Object.defineProperty function

After reviewing all the suggested answers, including: in Typescript, can Object.prototype function return Sub type instance? I still couldn't find a solution, so I'm reaching out with a new question. My goal is to replicate Infix notation in J ...

What is the process of parsing a Java property file in AngularJS?

Is it possible to access a properties file from an AngularJS application that is located outside of the web server? For example, in Java we can access a property file deployed separately from the project. Is there a way to achieve this in AngularJS? I at ...

What is the reason for Typescript's lack of ability to narrow down object keys to

When I establish a type in the following way: type O = { [k: string]: any }; I assume that the keys in O will be limited to the type string. However, if I create: type KO = keyof O; KO ends up being string|number. Why is this the case? Even though I sp ...

The updated value in useState keeps reverting to its original value

I am currently developing an ecommerce website using Next.js. In the product grid section, I have implemented a filter option that enables users to select a specific category. However, I am facing challenges with updating the products variable when I try t ...

When I utilize a component to create forms, the React component does not refresh itself

One of the components I am working with is a form handling component: import React, { useState } from "react"; export const useForm = (callback: any, initialState = {}) => { const [values, setValues] = useState(initialState); const onCha ...

A deep dive into TypeScript: enhancing a type by adding mandatory and optional fields

In this scenario, we encounter a simple case that functions well individually but encounters issues when integrated into a larger structure. The rule is that if scrollToItem is specified, then getRowId becomes mandatory. Otherwise, getRowId remains option ...

Organizing an array based on designated keywords or strings

Currently, I am in the process of organizing my logframe and need to arrange my array as follows: Impact Outcomes Output Activities Here is the initial configuration of my array: { id: 15, parentId: 18, type: OUTPUT, sequence: 1 }, { id: 16, parentId: ...

Listening for combinations of keys pressed using HostListener

I've been attempting to detect when a user presses the Shift+Tab key combination on the keyboard, but for some reason I can't get the event to trigger. @HostListener('keyup', ['$event']) @HostListener('keydown', [&a ...

Creating an Ionic 2 hybrid mobile app with a navigation controller: Step-by-step guide

While Angular 2 has been officially released, Ionic 2 is still in beta. I'm on the lookout for some great examples of Ionic 2 Hybrid Mobile Apps with a Navigation Controller. I stumbled upon this helpful tutorial at Icoderslab. Are there any other res ...

Animate specifically new items in a list rendered via ngFor in Angular animation

I am working with a list of array items in an ngFor loop. I have a basic button that adds an item to the array. My goal is to apply an animation only to the newly added items, but currently all the existing list items also receive the animation upon page l ...

Properly passing props to child components in React with TypeScript. Resolve Error ts(2322)

I am facing an issue where my code works perfectly in Javascript, but encounters problems when converted to Typescript. Despite the complexity of the question, it actually boils down to a simple query. I just wanted to share the functional JS code as a sol ...

In AngularJS, the $http get method is displaying the status code of the data object instead of

After pondering over this issue for several days, I am still unable to pinpoint what I am doing wrong. Any suggestions or insights would be greatly appreciated. My challenge lies in trying to showcase the response from a rest service to the user by utilizi ...

What is the best way to remove a reactive FormControl when a Component is destroyed?

I am facing an issue with a component that has a FormControl and a subscription to its value change: @Component(...) export class FooComponent implements OnInit, OnDestroy { fooFormControl: FormControl; ... ngOnInit() { this.fooFormControl.val ...

Typescript sometimes struggles to definitively determine whether a property is null or not

interface A { id?: string } interface B { id: string } function test(a: A, b: A) { if (!a.id && !b.id) { return } let c: B = { id: a.id || b.id } } Check out the code on playground An error arises stating that 'objectI ...

Clearly defining the data types for static dictionary values, while also deducing the precise structure or at least the keys

My goal is to create a static dictionary that is defined as a single object literal. I want to: Specify the type of values explicitly for typechecks and IDE suggestions Still have the ability to infer the exact shape, or at least keys I can achieve the f ...

Error Styling: Using CSS to Highlight Invalid Checkboxes within a Group

Is there a way to create a bordered red box around checkboxes that are required but not selected? Here is the code I currently have: <div class="fb-checkbox-group form-group field-checkbox-group-1500575975893"> <label for="checkbox-group-15005 ...

Incorporating Angular 2 Templates: Exploring how to bind keydown.arrow event to the entire div rather than just specific

I am currently working on a simple navigator component that includes buttons and a date-picker subcomponent. My goal is to be able to bind keydown.arrowleft etc. for the entire div, which has a CSS style of 100% height and width. Below is the template stru ...

What could be the reason for the GET method being executed after the DELETE method in ExpressJS?

Whenever I trigger the DELETE method in my Express app, it seems that the GET method is automatically invoked right after. This results in an error within my Angular code stating that it expects an object but receives an array instead. Why is the GET meth ...

The art of Angular localization: harvesting language elements from ts documents

Is it true that $localize is only available for translation in ts files, but the extraction of strings is currently only implemented for templates? I came across information stating that the extraction of strings from ts files should be possible in Angular ...

Exploring the capabilities of Angular, I delved into the concept of utilizing a multid

I am working with a multidimensional array in Angular html <table> <tr *ngFor="let x of multi"> <td>{{x}}</td> <td>{{x}}</td> <td>{{x}}</td> </tr> </table&g ...