我正在学习使用rxjs中的Subject将字符串发送到后端。我从前端管理字符串,它会打印期望的字符串。当我查看net core2.1时,它以null而不是字符串形式发送。
这是googlechrome开发人员窗口“ 2:65418 / api / stock:1 POST http://localhost:65418/api/stock 500(内部服务器错误)”中的错误
这是stock.component.ts文件中的组件代码:
post(stock){
this.api.postStock(stock);
console.log("this is the stock I posted", stock);
}
这是api服务ts代码:
import { Injectable } from '@angular/core'
import { HttpClient, HttpHeaders } from '@angular/common/http'
import { Subject} from 'rxjs'
import { Cagr } from '../_models/cagr'
import { environment } from '../environments/environment'
const headers = new HttpHeaders({'Content-Type':'application/json; charset=utf-8'});
@Injectable({
providedIn: 'root'
})
export class ApiService {
apiURL = environment.apiUrl;
stockUrl = this.apiURL + '/api/stock';
private icarg = new Subject<Cagr[]>();
Icagr$ = this.icarg.asObservable();
constructor(private http: HttpClient) { }
postStock (stocks) {
this.http.post(this.stockUrl, stocks, {headers:
headers}).subscribe(res => {
console.log("POST call successful value returned in body", res);
},
response => {
console.log("POST call in error", response);
})
}
这是组件html代码:
<mat-card>
<mat-card-content>
<form class="example-form">
<mat-form-field >
<input [(ngModel)]="stock" name = "stock" matInput placeholder="Input stock symbol" >
</mat-form-field>
</form>
</mat-card-content>
<mat-card-actions>
<button (click)="post(stock)" mat-raised-button color="primary">Go</button>
<div>
这是我希望在其中看到字符串的后端区域,但为空:
[HttpPost]
public async Task<IActionResult> Post([FromBody]Models.Stock stock)
{
_context.Stocks.Add(stock);
await _context.SaveChangesAsync();
return Ok(stock);
}
答案 0 :(得分:1)
首先,您是否使用邮递员/提琴手/挥舞者之类的任何工具测试了api。因为据我所知,内部服务器错误来自您的api。