我正在尝试将一个大对象从dotnet核心api端点发送到要加载的浏览器,但出现上述错误。该请求适用于邮递员,并返回对身体的成功响应,但是在角度/离子应用程序上失败。
这是我用角度写的邮政编码。
authentication.service.ts
authenticate(mobile: string, password: string): Observable<Account>{
var CorrelationId = Guid.newGuid();
let headers = new HttpHeaders({
'Content-Type': 'application/json',
'Accept': '*/*',
'CorrelationId': CorrelationId,
'Access-Control-Allow-Origin': '*'
});
let postData = {"username": "0619601196", "pwd": "test" }
return this.httpClient.post<Account>('http://localhost:5000/authenticate', postData,
{headers: headers, responseType: 'json'})
.pipe(catchError(this.handleError));
}
login-form.page.ts
login(){
this.authentication.authenticate(this.mobileNumber, this.password).subscribe(data => {
console.log('UserToken: ' + data.userToken);
});
}
答案 0 :(得分:0)
[更新]我找到了解决方法,
原因:客户端未完成全部数据加载时响应被截断,因此这就是为什么我得到状态码200(确定)但有错误的原因。
解决方案:在我的dotnetcore API中,我为响应添加了一个内容长度的标头,以在关闭请求之前通知客户端需要多少数据
WebClient client = new WebClient();
client.Encoding = System.Text.Encoding.UTF8;
string site = client.DownloadString($"https://esel.at/api/termine/data?date=05.09.2020&selection=false");
var myJsonObject = JsonConvert.DeserializeObject<List<MyJsonType>>(site);
foreach(var myJosnType in myJsonObject)
{
//add your logic here
Console.WriteLine(myJosnType.title);
}