angular - Angular8 使用 HttpClient GET 方法从 Scala Play 端点检索连续数据
问题描述
我在将数据(连续)从 Scala Play 环境接收到 Angular 8 前端项目时遇到问题。对于更简单的数据样本(静态 jsons),访问数据(GET 和 POST 方法都没有参数)没有问题,但是当我尝试从 Scala 中实现的端点获取当前时间时,它就是不工作。正如我使用 curl 命令测试的那样,Scala 的代码可以正常工作,并返回预期的结果。
在 Angular 中,我有一项服务向 Scala 和我订阅服务功能的主要组件发出请求。应用服务.ts
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { map } from 'rxjs/operators';
import { Observable } from 'rxjs/index';
@Injectable()
export class AppService {
private getTime = '/api/liveClock';
constructor(private http: HttpClient) {
}
public retrieveTime() {
return this.http.get(this.getTime, {responseType: 'text'}).pipe(
map(response => response)
);
}
app.component.ts
import { Component, OnInit } from '@angular/core';
import { AppService } from './app.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
constructor(private appService: AppService) {
this.appService.retrieveTime().subscribe({
next(t) { console.log('d:', t); },
error(e) {console.log('error:', e); },
complete() { console.log('Finished'); }
});
}
onInit() {
}
}
你对这项工作有什么建议吗?非常感谢!
解决方案
我找到了解决我的问题的方法 - 服务请求功能的不同实现:
public retrieveTime(): Observable<any> {
const req = new HttpRequest(
'GET', `${this.getTime}`, {responseType: 'text', reportProgress: true}
);
return this.http.request(req).pipe(
map(response => response)
);
}
如果有人有不同的想法,请赐教:-)
推荐阅读
- python - 在 python 代码中插入 bash 脚本
- javascript - 如果从不关闭握手,WebSockets 不会在 IE 上关闭
- r - 将列表的每个元素保存到文本文件
- angular - Angular中请求参数的条件拦截器操作
- c# - 对方法进行单元测试并获取未将对象引用设置为对象实例
- android - Android Signed Apk 在 5.0 以下版本中崩溃
- typescript - 如何使用 RequireJS loader 在 VS 2017 中创建 typescript 项目以使用 Redux
- wordpress-theming - Gutenberg 模板:核心块属性
- javascript - 使用流写入和读取文件
- shell - Copy command issue in Macbook Air