【发布时间】:2021-12-18 12:47:04
【问题描述】:
我目前正在尝试使用 Angular 的服务器创建一个创建函数(以进行报价)。 所以我在 html 文件中输入了一个表单。
<form [formGroup]="addquoteform" (submit)="createquote()">
<p>
<mat-form-field appearance="outline">
<mat-label>quote</mat-label>
<input matInput placeholder="Placeholder" formControlName="quote">
<mat-icon matSuffix>sentiment_very_satisfied</mat-icon>
</mat-form-field>
</p>
<p>
<button mat-raised-button color="primary">Submit</button>
</p>
</form>
然后我在.ts文件中做了这个函数。
createquote(){
this.dbService.addquote(this.addquoteform.value)
.subscribe(data => {
console.log("new quote")
}, err => {
console.log(err);
})
}
然后我在 server/quote.ts 中做了报价
interface QuoteData {
quote : string;
}
export interface quote extends QuoteData{
}
然后在 services/dbService.ts 文件中我创建了 this.http.post()
addquote(quoteObj: any) {
return this.http.post(this.baseUrl + 'quote', quoteObj);
}
但最后一个不起作用。
我得到的只是错误:
POST http://localhost:4200/quote 404 (Not Found)
我尝试更改链接,但似乎没有任何帮助。
知道是什么原因造成的吗?
【问题讨论】:
-
baseUrl 是否正确? 4200 通常是 Angular 应用程序端口,而不是 Web 服务端口。
-
在某个端口('8080' 或 smt)上侦听神秘的 API(节点 + 可能是 express / typescript)=> 所以将您的数据发布到 localhost:8080(不要忘记 cors)或smt 而不是 ng 开发服务器端口(除非您为 API 设置代理)...
-
@LucaFarsetti 感谢您的回复,但我认为问题不在于 baseUrl,因为我尝试更改 baseUrl 很多但没有任何改变。
标签: angular database typescript server crud