angular - .NetCore Angular:“解析值时遇到意外字符:c.路径'',第 0 行,位置 0。” 仅在 Angular PUT 请求期间
问题描述
我正在使用 asp.net core 2.2 和 angular 8 开发一个简单的新网络。我总是得到
HttpErrorResponse {headers: HttpHeaders, status: 400,
statusText: "Bad Request", url: "https://localhost:5001/api/session/1",
ok: false, …}
error:
detail: "The inputs supplied to the API are invalid"
errors:
"": ["Unexpected character encountered while parsing value: c. Path '',
line 0, position 0."]
当我尝试通过邮递员向我的后端发出请求时,一切似乎都正常。
**public async Task<Session> UpdateSession(Session session)
{
if(session == null)
{
throw new InvalidOperationException("Session not found");
}
_db.Sessions.Update(session);
var saved = false;
while (!saved)
{
try
{
await _db.SaveChangesAsync();
saved = true;
}
catch (DbUpdateConcurrencyException ex)
{
foreach (var entry in ex.Entries)
{
if (entry.Entity is Session)
{
var prosposedValue = entry.CurrentValues;
var databaseValue = entry.GetDatabaseValues();
foreach (var property in
- prosposedValue.Properties)
{
var prosposedValues =
prosposedValue[property];
var databaseValues =
databaseValue[property];
}
entry.OriginalValues.SetValues(databaseValue);
}
else
{
throw new NotSupportedException(
"Don't know how to handle consurrency
conflicts for "
+ entry.Metadata.Name);
}
}
}
}
return session;
}
[HttpPut("{sessionId}")]
public async Task<IActionResult> UpdateSession([FromBody] Session
session)
{
try
{
if (ModelState.IsValid)
{
if (session.id > 0)
{
await context.UpdateSession(session);
return Ok();
}
return BadRequest();
}
else
{
throw new InvalidOperationException();
}
}
catch (Exception ex)
{
throw new NullReferenceException(ex.Message.ToString());
}
}
updateSession(id:number, session: Cl_session) {
const Id = `${this.url}/${id}`;
return this.http.put<Cl_session>(Id, Cl_session)
.pipe(
catchError(this.appConfig.handleError)
); }
if(this.mysession.id) {
this.isLoading = true;
console.log(this.mysession); // using this printed the values in
the console
this._session.updateSession(this.mysession.id,
this.mysession).subscribe(()=> {
this.message = "Session updated successfully";
setTimeout(() => {
this.isLoading = false;
this.router.navigate(['/session/list']);
}, 500);
}, (error) =>{
console.log(error);
//this.appConfig.handleError(error);
});**
解决方案
诸如“解析值时遇到意外字符:c. Path '', line 0, position 0.”之类的错误。几乎总是 JSON 解析错误,通常是在被反序列化的字符串实际上不是有效的 JSON 时引起的。这里没有足够的客户端代码来正确诊断实际原因是什么,但请查看Cl_session
.
推荐阅读
- opencv - Linux, ubuntu 18.04 , Yolov4 darknet , 视频流停止了!(在我的电脑上,不是谷歌 colab)
- python - 如何填充pyplot中两个平均值之间的区域?
- javascript - 等待 Promise.all 中的 array.map 迭代
- c# - 使用 C# 将 dll 注入当前进程
- java - JPA Persist 不持久化子实体
- java - JAVA 10+ -XX:MaxRAMPercentage 标志在 Kubernetes 容器中
- python - 将拆分的数据系列连接到图表中
- python - SQL LIKE 在查询后无法使用空格
- wso2si - Kibana - 匹配此索引模式的索引不包含任何时间字段
- python - Python 递归问题(决策树)