gpt4 book ai didi

Angular 5 - 添加标题时的问题

转载 作者:行者123 更新时间:2023-12-05 07:35:36 26 4
gpt4 key购买 nike

我有这个...myservice.service.ts:

import { HttpClient } from '@angular/common/http';
import { HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { ReqLogin, ResLogin, ResList} from '../../interfaces/general';
import { GlobalService} from '../global/global.service';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/do';

...

list(): Observable<ResList> {
const url = this.gs.backendServer + this.gs.backend.endpoints.list;
const httpOptions = {
headers: new HttpHeaders({
'Authorization': this.gs.token
})
};
return this.http.get(url, httpOptions)
.map(res => <ResList> res)
.do(dataReceived => console.log(dataReceived));
}

这工作正常,服务器接收读取器,我从服务器接收数据,但我也收到此异常:

ERROR TypeError: Cannot read property 'length' of undefined
at eval (http.js:123)
at Array.forEach (<anonymous>)
at HttpHeaders.lazyInit (http.js:117)
at HttpHeaders.init (http.js:265)
at HttpHeaders.forEach (http.js:368)
at Observable.eval [as _subscribe] (http.js:2172)
at Observable._trySubscribe (Observable.js:172)
at Observable.subscribe (Observable.js:160)
at subscribeToResult (subscribeToResult.js:23)
at MergeMapSubscriber._innerSub (mergeMap.js:138)

如果我删除 Http 选项,这就完美了。

有什么线索吗?

最佳答案

尝试使用来自@angular/http 库的 Headers 和 RequestOptions。

ApplicationService.ts

import { Injectable } from '@angular/core';
import { Http, Headers, Response, RequestOptions } from '@angular/http';

@Injectable()
export class ApplicationService {

constructor(private http: Http) {
}

myformPost(id:number, formData : any){

let header = this.initHeaders();
let options = new RequestOptions({ headers: header, method: 'post'});
let body = JSON.stringify(formData);

return this.http.post(this.myapiUrl, body, options)
.map(res => {
return res.json();
})
.catch(this.handleError.bind(this));
}

private initHeaders(): Headers {
var headers = new Headers();
let token = localstorage.getItem(StorageKey.USER_TOKEN);
if (token !== null) {
headers.append('Authorization', token);
}

headers.append('Pragma', 'no-cache');
headers.append('Content-Type', 'application/json');
headers.append('Access-Control-Allow-Origin', '*');
return headers;
}

private handleError(error: any): Observable<any> {
return Observable.throw(error.message || error);
}
}

关于Angular 5 - 添加标题时的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49452538/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com