1

I creates an application by using angular 7 and asp.net core when i when create the login page, i get this errorr: error: SyntaxError: Unexpected token L in JSON at position 0 at JSON.parse () at XMLHtt

enter image description here

login.service

UserLogin(logModel: LoginModel): Observable<LoginModel> {
  return this.http.post<LoginModel>(this.url + 'Login', logModel, this.headers).pipe();
}

and this is my component, login.ts

 login(){
if(this.loginForm.valid){
this.VaildateModel();
this.LoginService.UserLogin(this.logModel).subscribe(success =>{
this.route.navigate(['home']);
}, err => {
  console.log(err);
  this.message = err.error;
  
}
)
}
}

and this my function in web api app

       [HttpPost]
    [Route("Login")]
    public async Task<IActionResult> Login(LoginModel model)
    {
        if (model == null)
            return NotFound();
        var user = await _manager.FindByEmailAsync(model.Email);
        if (user == null)
            return NotFound();
        //if (!user.EmailConfirmed)
        //    return Unauthorized("Email is not Confirmed yet");
        var result = await _signInManager.PasswordSignInAsync(user, model.Password, model.RememberMe, true);
        if (result.Succeeded)
        {
            return Ok("Login Success");
        }
        else if (result.IsLockedOut)
        {
            return Unauthorized("User Account is locked");
        }

        return StatusCode(StatusCodes.Status204NoContent);
    }

can anyone help me please :( ?

Roman Kalinchuk
  • 718
  • 3
  • 14
  • Does this answer your question? ["SyntaxError: Unexpected token < in JSON at position 0"](https://stackoverflow.com/questions/37280274/syntaxerror-unexpected-token-in-json-at-position-0) – R. Richards Jun 21 '20 at 21:52
  • It looks like there's an attempt to parse the response as JSON but the response is actually just raw text. Is the angular application expecting a response that contains JSON instead of just raw text? I'd i imagine that the response doesn't match the structure of `LoginModel`, can you post the code for that class? – Charleh Jun 21 '20 at 22:01
  • @Charleh loginModel in angular export class LoginModel { email: string; password: string; rememberMe: boolean; } – Mostafa Gamal Jun 21 '20 at 22:22
  • @Charleh public class LoginModel { [StringLength(256), Required] public string Email { get; set; } [Required] public string Password { get; set; } [Required] public bool RememberMe { get; set; } } – Mostafa Gamal Jun 21 '20 at 22:23
  • Apparently you should be returning JSON in your response, not a raw string. The httpclient in angular expects a response as a JSON object by default. You need to specify the accept/content type header in order to let it know what type of data is expected in the response. See this: https://stackoverflow.com/questions/50826531/httpclient-post-tries-to-parse-a-non-json-response – Charleh Jun 21 '20 at 22:31

1 Answers1

0

You need to install Microsoft.AspNetCore.Mvc.NewtonsoftJson nuget package.

Add this code in Program.cs file

builder.Services.AddControllers().AddNewtonsoftJson();

then, Add attribute above your controller [Produces("application/json")].

this will work fine.

read this article:

https://learn.microsoft.com/en-us/aspnet/core/web-api/advanced/formatting?view=aspnetcore-6.0

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49