Hey everyone sorry to bother you, but I have not been able to find a comprehensible way to use headers with an http get request in angular 2 (Iāve been looking for 2+ days)
this is my code thus far (though, obviously, it does nothing at the moment):
request(value){
var headers = new Headers();
headers.append('Authorization', 'Bearer ' + this.token);
this.http.get( this.URL + "product/" + value, {"Headers": headers})
.map(
res => res.json()
)
.subscribe(
data => this.product = JSON.stringify(data),
err => this.status.error = JSON.stringify(err)
);
}
If anyone with experience in this topic would be willing to give some guidance, It would be much appreciated
Thanks !
First thing that jumps out to me is that youāve capitalized Headers
in the options argument. I think it should just be headers
.
No luck there, but thanks for the input
It would help to know how you are invoking this code, what you are expecting to see, and what you are seeing.
So, the company I work for has recently made an API to view products.
We already have some angular 1 in a web page that is able to successfully retrieve the product, by using a product ID and a JWT token:
$http({ method: 'GET', url: $scope.config.url + 'product/' + $scope.product.id, headers: { 'Authorization': 'Bearer ' + $scope.store.token }, withCredentials: true }).then(function(result) { if (result.data.error) { $scope.status.error = result.data.error; } else if (result.data.products) { $scope.status.error = null; $scope.products = result.data.products; } else { $scope.status.error = result.data; } }
Within the webpage itās shown with {{products | json}} and has an output like:
[ { āproduct_idā: ā12345ā, ātitleā: āName of Productā, āshow_priceā: ātrueā, āgeneral_imagesā: [ ā00142201005080924314395.jpgā ] } ]
I was able to get a token on an earlier page in the app, which I pushed to this page via NavParam,
and the āValueā in my function is just the product ID that the user is searching for.
Iām attempting to translate it to angular 2, so I can retrieve the product info and display it within a native looking app, but no dice thus far
Your res object has headers and all response data. Just donāt use .map(res => res.json())
All right, I commented that portion out of the function, as well as added alert functions to the .subscribe portion for success or error. Unfortunately, neither of those alerts pop up when I run the code, so it seems that the problem is still there, and is preventing that portion of the code from running at all.
I changed
request(value){ var headers = new Headers(); headers.append('Authorization', 'Bearer ' + this.token); this.http.get( this.URL + "product/" + value, {"Headers": headers}) .map( res => res.json() ) .subscribe( data => this.product = JSON.stringify(data), err => this.status.error = JSON.stringify(err) ); }
into
request(value){ var headers = new Headers(); headers.append('Authorization', 'Bearer ' + this.token); this.http.get( this.URL + "product/" + value, {"headers": headers}) /*.map( res => res.json() )*/ .subscribe( data => alert('data'), //this.product = JSON.stringify(data), err => alert('error') //this.status.error = JSON.stringify(err) ); }
So, I would be able to see if the .subscribe() portion ran, but it doesnāt appear to be
Try check it into dev console -> network. If the request has been sent, and what is response.
Alright, just checked, and the request is not sent
I found the error (i Believe) in the web console:
EXCEPTION: Error: Uncaught (in promise): TypeError: Cannot read property 'get' of undefined
Has anyone seen this one before?
constructor (public http: Http)
You are either not initializing this.http
or you are invoking request
in a context where this
is not what you expect it to be.
OK, so this is what I have thus far in the initialization of Http:
import {Http, Headers} from 'angular2/http';
And
static get parameters() { return [[NavController],[NavParams],[Http]]; }
And
constructor(nav: NavController, params: NavParams, bcScanner: BarcodeScanner, public http: Http) { this.nav = nav; this.params = params; this.picturesPage = PicturesPage; this.token = this.params.get("token") this.bcScanner = bcScanner; this.http = http; }
And the function is within the pages class. I thought I hit all of my bases, but evidently Iām still missing something here
Java-like developer detected.
I also find this incredibly unintuitive, but in the land of JavaScript this
is not guaranteed to be the object that defines the method (especially if you pass functions around).
OK, Iāll take a look into that Thanks
(and Iāve never actually used Java, so I almost laughed out loud at that (before I remembered I was in the workplace )
One other potential area of confusion is whether youāre using TypeScript or JavaScript. If TypeScript, you donāt need that static get parameters method. Donāt know if it would actually cause harm or not, but it might be worth ensuring that the http parameter to the constructor is coming in properly.
Iām using typescript so Iāll check that out The reason I have it there is because on the previous page, I was using an http post, which absolutely refused to run until I had put it in on that page
would be a lot easier to debug/resolve if you put the code in plnkr or jsbin