Angular CLI对localhost:3000的post请求不工作,可能是我的代理配置有问题
我创建了一个只处理post请求的基本本地API。 我试图在angular应用程序上实现post请求,但它返回了一些奇怪的东西。显然有些事情我做得不对。 我有理由相信该应用程序正在达到API,但它没有返回我期望的结果。 这是我的密码: 1/Angular CLI对localhost:3000的post请求不工作,可能是我的代理配置有问题,angular,typescript,Angular,Typescript,我创建了一个只处理post请求的基本本地API。 我试图在angular应用程序上实现post请求,但它返回了一些奇怪的东西。显然有些事情我做得不对。 我有理由相信该应用程序正在达到API,但它没有返回我期望的结果。 这是我的密码: 1/app.component.html import { Component } from '@angular/core'; import { HttpClient } from '@angular/common/http'; @Component({ se
app.component.html
import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'Welcome to the parentheses balance checker';
result;
test = "hello";
constructor(private http: HttpClient) {}
onSubmit(textEnter: string) {
let json = { "string": textEnter };
console.log(json);
this.result = this.http.post('http://localhost:3000/parentheses/', json);
}
}
2/app.components.ts
点击这里
4/我试图达到的API基本上是一个括号检查器:
module.exports = function (string) {
const openArray = ["(", "[", "{", "<"];
const closeArray = [")", "]", "}", ">"];
let i = 0, parOpen = 0, openingPar= "", closingPar = [], erroneos = "";
for(i; i < string.length; i++) {
if (openArray.includes(string[i])) {
openingPar += string[i];
parOpen++;
closingPar.push(closeArray[openArray.indexOf(string[i])]);
}
if (closeArray.includes(string[i])) {
let erroneos = string[i];
parOpen--;
if (string[i] === closingPar[closingPar.length - 1]) {
closingPar.pop();
} else {
return { result: false, preceeding: openingPar, erroneos: erroneos };
};
};
if (parOpen < 0) return { result: false, preceeding: openingPar, erroneos: erroneos };
}
return { result: (parOpen === 0 ? true : false), preceeding: openingPar, erroneos: erroneos };
};
5/它所指的Javascript函数:
{
*_isScalar*: false,
*source*: {
*_isScalar*: false,
*source*: {
etc..
我不知道该怎么解释。无论何时尝试从网络上访问某个内容,使用http都会返回一个可观察的结果。
因此,您需要订阅该可观测数据,以便能够在数据可用时读取数据:
你的代码是这样的:
this.http.post('http://localhost:3000/parentheses/', json).subscribe((data) => {
this.result = data;
console.log(this.result);
});
但应该是这样的:
<div>
<form>
<input #textEnter placeholder="text">
<button type="submit" (click)="onSubmit(textEnter.value)">click here</button>
</form>
<pre>
{{ result | async | json }}
</pre>
</div>
一些参考资料:
您也可以在html中使用async
管道来代替订阅。比如:
点击这里
{{result | async | json}
什么API?它会给其他客户带来什么回报?你期望什么呢?我会把它添加到我的问题中,在这里我更新了它。谢谢您正在显示可观察对象的JSON。我强烈建议您通读并阅读规范,以了解潜在的问题。也可以使用TypeScript-如果结果
有一个合适的类型,编译器会告诉你这没有意义。我对这一切都很陌生,所以我有点迷茫。我将仔细阅读你建议的文件。谢谢
this.result = this.http.post('http://localhost:3000/parentheses/', json);
this.http.post('http://localhost:3000/parentheses/', json).subscribe((data) => {
this.result = data;
console.log(this.result);
});
<div>
<form>
<input #textEnter placeholder="text">
<button type="submit" (click)="onSubmit(textEnter.value)">click here</button>
</form>
<pre>
{{ result | async | json }}
</pre>
</div>