首页 > 解决方案 > .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);
       });**

标签: angularasp.net-core-webapi

解决方案


诸如“解析值时遇到意外字符:c. Path '', line 0, position 0.”之类的错误。几乎总是 JSON 解析错误,通常是在被反序列化的字符串实际上不是有效的 JSON 时引起的。这里没有足够的客户端代码来正确诊断实际原因是什么,但请查看Cl_session.


推荐阅读