json - 我收到“未捕获的类型错误:无法读取未定义的属性 'get'”
问题描述
我正在尝试使用不同的端口从另一个微服务(本地)检索 json。我不确定问题出在我的配置还是微服务上。当我点击 url 时,我确实让 json 显示在页面上。
this.http.get('http://localhost:8081/*****', { responseType: 'json' }).subscribe(data => console.log(data));
*****
= 获取 json 的页面
我期待一个 json 但是我收到一条错误消息。
解决方案
你应该有这样的东西:
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
@Injectable()
export class TestService {
constructor(private httpClient: HttpClient) { }
public getData(): Observable<any> {
const apiURL = `http://localhost:8081/*****`;
const opts = {
responseType: 'json'
};
return this.httpClient.post(apiURL, opts);
}
}
从您的组件:
import { TestService} from '..test.service';
export class TestClass {
constructor(private testService: TestService) { }
getInfo() {
this.testService.getData().subscribe(data => {
console.log('***TEST***' + data);
});
}
}
推荐阅读
- javascript - jQuery Accordion 在展开时自动选择所有包含的文本
- node.js - 向 qnamaker bot 添加对话框的最有效方法
- c# - 使用 C# 向量
SIMD查找匹配元素的索引 - wordpress - 可以替换 WP 包含目录吗?
- python - 本地主机在使用瓶子 python 时不工作
- dynamics-crm - 无法将关系添加到解决方案
- microsoft-graph-api - 如何限制应用哪些应用程序权限的用户?
- c# - 为什么默认 Asp.Net Core 引用“cdnjs.cloudflare.com”?
- python - 如果没有输入,哪些 Django 模型字段不会引发 IntegrityError?
- java - Java keytool importcert:非法选项