javascript - Angular http.post 不通过订阅发送数据
问题描述
我正在尝试将数据发布到我的 api。post.subscribe 不发送任何数据,也没有抛出错误。API 100% 正常工作。
这是我的代码:
httpservice.ts
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Product } from './Product';
import { Observable, of } from 'rxjs';
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root',
})
export class HttpService {
baseURL = 'https://localhost:2403/testapi/';
constructor(private http: HttpClient) {
this.products = new Array();
}
products: Product[];
post(product: Product): boolean {
if ( !this.checkIfProductExistsAlready(product) ) {
console.log('posting product');
this.http.post<any>(baseURL,{"product": product.name, "price": 10, "done": false})
.subscribe((data) => {
console.log(data);
product.id = data.id;
console.log('hi');
},
error => console.log('uojdsigdk' + error)
);
console.log('posted ' + product.id);
this.products.push(product);
return true;
} else {
return false;
}
}
form.component.ts
addItem(): void {
this.isError = false;
if (!this.httpservice.post(new Product(-1, this.name, this.price, 0))) {
this.isError = true;
}
}
这是 app.module.ts 中的提供者声明
[...]
providers: [HttpService],
[...]
这可能是由配置文件引起的吗?
解决方案
也许这是因为您尝试通过 http 访问本地网络服务器?
baseURL = 'https://localhost:2403/testapi/';
否则使用提琴手,在你的 api 上做一个发布请求,看看服务器返回了什么。:)
推荐阅读
- process - Gunicorn:检查正在运行的进程的命令行参数。(高山 Linux)
- javascript - Vuex4 - 无法读取未定义的属性(读取“状态”)
- sql - 的概念
输入SQL? - amazon-web-services - 为什么我无法在 AWS Cognito 控制台中选中复选框?
- swift - 当用户在我的应用程序后台时,当计时器达到 0 时,我将如何显示通知?
- reactjs - 在单元格中换行
- c++ - 析构函数数组堆栈tiwer hanoi c++双指针
- sql - 基于另一列 min/max 的每种类型的 SQL 返回差异
- python - 将函数应用于 2D Numpy 数组以创建新的简化数组
- python - 我使用什么函数来遍历 URL 参数并将其显示在 Pandas 数据框中?