Questions tagged «angular-httpclient»


1
向Angular HttpClient添加HTTP标头不发送标头,为什么?
这是我的代码: import { HttpClient, HttpErrorResponse, HttpHeaders } from '@angular/common/http'; logIn(username: string, password: string) { const url = 'http://server.com/index.php'; const body = JSON.stringify({username: username, password: password}); const headers = new HttpHeaders(); headers.set('Content-Type', 'application/json; charset=utf-8'); this.http.post(url, body, {headers: headers}).subscribe( (data) => { console.log(data); }, (err: HttpErrorResponse) => { if (err.error instanceof Error) …

8
Angular 4 HttpClient查询参数
我一直在寻找一种通过new将查询参数传递到API调用中HttpClientModule的方法HttpClient,但尚未找到解决方案。使用旧Http模块,您将编写如下内容。 getNamespaceLogs(logNamespace) { // Setup log namespace query parameter let params = new URLSearchParams(); params.set('logNamespace', logNamespace); this._Http.get(`${API_URL}/api/v1/data/logs`, { search: params }) } 这将导致对以下URL的API调用: localhost:3001/api/v1/data/logs?logNamespace=somelogsnamespace 但是,新HttpClient get()方法没有search属性,因此我想知道在哪里传递查询参数?

11
捕获Angular HttpClient中的错误
我有一个数据服务,看起来像这样: @Injectable() export class DataService { baseUrl = 'http://localhost' constructor( private httpClient: HttpClient) { } get(url, params): Promise<Object> { return this.sendRequest(this.baseUrl + url, 'get', null, params) .map((res) => { return res as Object }) .toPromise(); } post(url, body): Promise<Object> { return this.sendRequest(this.baseUrl + url, 'post', body) .map((res) => { return …

6
Angular HttpClient“解析期间Http失败”
我尝试从Angular 4向我的Laravel后端发送POST请求。 我的LoginService具有以下方法: login(email: string, password: string) { return this.http.post(`http://10.0.1.19/login`, { email, password }) } 我在我的LoginComponent中订阅了此方法: .subscribe( (response: any) => { console.log(response) location.reload() }, (error: any) => { console.log(error) }) 这是我的Laravel后端方法: ... if($this->auth->attempt(['email' => $email, 'password' => $password], true)) { return response('Success', 200); } return response('Unauthorized', 401); 我的Chrome开发人员工具说我的请求成功,并带有200个状态代码。但是我的Angular代码触发了该error块并给出了以下消息: 解析http://10.0.1.19/api/login期间的Http失败 如果我从后端返回一个空数组,则可以正常工作...所以Angular试图将我的响应解析为JSON?我如何禁用此功能?
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.