javascript - values.join is not a function in the file http.es5.js Angular 4 - Stack Overflow

In angular 4 I have used a code snippet like;let h = [{Content: 'applicationjson'}];this.htt

In angular 4 I have used a code snippet like;

let h = [{Content: 'application/json'}];

this.http.post(server, this.userDataObj, {headers: h}).....

So basically I wanted to add a header in my ajax call.

Now I am constantly getting an error

ERROR TypeError: values.join is not a function

I inspected the error location and I found at the file http.es5.js there is a code like;

 req.headers.forEach(function (name, values) {
      return xhr.setRequestHeader(name, values.join(',')); 
 });

Now I added a console.log(req.headers) just above the snippet, and I got ;

 [Object]
      0:Object
         Content: 'application/json'

Now as far as I know function inside forEach loop on array in JS takes second argument(which is values in http.es5.js) is the index of the element. And I tested via console.log(values) and I got result 0. So it is natural that values.join is not a function, and it will never be a function on an integer.

I tried ;

var headers = new Headers();
headers.append("Content", 'application/json');

this.http.post(server, this.userDataObj, {headers: headers}).subscribe(.....

Which also giving me same error.

Tried this;

var h = new Headers();
h.append("kkk", 'aaa');
let options = new RequestOptions({ headers: h });

this.http.post(server, this.userDataObj, options).subscribe(.....

Still same error.

Is it a bug? Or am I doing any mistake? Any idea will be very helpful for me.

PS: I am new to Angular4.

In angular 4 I have used a code snippet like;

let h = [{Content: 'application/json'}];

this.http.post(server, this.userDataObj, {headers: h}).....

So basically I wanted to add a header in my ajax call.

Now I am constantly getting an error

ERROR TypeError: values.join is not a function

I inspected the error location and I found at the file http.es5.js there is a code like;

 req.headers.forEach(function (name, values) {
      return xhr.setRequestHeader(name, values.join(',')); 
 });

Now I added a console.log(req.headers) just above the snippet, and I got ;

 [Object]
      0:Object
         Content: 'application/json'

Now as far as I know function inside forEach loop on array in JS takes second argument(which is values in http.es5.js) is the index of the element. And I tested via console.log(values) and I got result 0. So it is natural that values.join is not a function, and it will never be a function on an integer.

I tried ;

var headers = new Headers();
headers.append("Content", 'application/json');

this.http.post(server, this.userDataObj, {headers: headers}).subscribe(.....

Which also giving me same error.

Tried this;

var h = new Headers();
h.append("kkk", 'aaa');
let options = new RequestOptions({ headers: h });

this.http.post(server, this.userDataObj, options).subscribe(.....

Still same error.

Is it a bug? Or am I doing any mistake? Any idea will be very helpful for me.

PS: I am new to Angular4.

Share Improve this question edited Aug 1, 2017 at 4:33 KOUSIK MANDAL asked Aug 1, 2017 at 4:10 KOUSIK MANDALKOUSIK MANDAL 2,0521 gold badge24 silver badges51 bronze badges 2
  • Headers aren't an array. They are RequestOptions object. This SO question should help stackoverflow./questions/41133705/… – Wainage Commented Aug 1, 2017 at 4:28
  • I tried the your mentioned question's accepted answer, still same error. What I tried mention in the question now.Please check. I bee clueless now. :( – KOUSIK MANDAL Commented Aug 1, 2017 at 4:34
Add a ment  | 

3 Answers 3

Reset to default 6

Angular 4.3:

import {HttpClient, HttpHeaders} from '@angular/mon/http';

You need to use Headers from angular/http to add the headers to the request.

first use those imports

import { Http, Headers} from '@angular/http';

then add the headers like this

var headers = new Headers();

headers.append("Content", 'application/json');

this.http.post(server, this.userDataObj, {headers: headers })
import { HttpClient, HttpHeaders } from '@angular/mon/http';
import { Api } from '../../providers/providers';


export class demo{

  response: any;     

  constructor(public api: Api){
     this.apiCall();
  }
  apiCall(){
     var headers = new HttpHeaders({
     'Content-Type': 'application/json',
     'token': 'token_demo'
      });

      this.response = this.api.post("services/user", {}, { headers: headers });
      this.response.subscribe((data) => {
        console.log('data : ' + data);
      }, err => {
        console.error("error = ", err);
      }
  }
}

this is working on ........

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745638144a4637522.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信