Uncaught SyntaxError: Unexpected end of input错误求助,类型不匹配是否有关?
Uncaught SyntaxError: Unexpected end of input with Mismatched Frontend/Backend Types First off, let's clear up your core question: the mismatched nuMes/nuAno types (string in frontend, short in backend) won't directly cause the Unexpected end of input syntax error. That error is almost always related to incomplete or invalid JavaScript/TypeScript code parsing, or more commonly in this scenario, invalid JSON being returned from your API. Let's break down the possible causes and fixes step by step.
Why Type Mismatch Isn't the Culprit Here
Type mismatches between frontend and backend will typically lead to type conversion warnings or runtime type errors (e.g., Angular's HttpClient trying to map a numeric value to a string property), but not a syntax parsing error. Syntax errors happen when the browser/TypeScript compiler can't parse your code at all—you've already ruled out missing brackets/braces, so we need to look elsewhere.
Likely Causes of Unexpected end of input
1. Invalid JSON Response from Your Backend
This is the most common culprit for this error in API-driven frontend apps. Let's look at your backend code:
- When
listaBoletim.Count == 0, you returnnull. Whilenullis valid JSON, assigning it directly to aBoletim[]array in your frontend can cause issues later (like in template rendering), but not a syntax error. - The bigger problem is your exception handling:
throw new Exception();in ASP.NET will default to returning an HTML error page (not JSON) with a 500 status code. When your frontend tries to parse this HTML as JSON, it hits invalid syntax immediately, triggering the "Unexpected end of input" error.
2. Implicit Template Syntax Issues
If your component's template uses *ngFor to iterate over item (e.g., <div *ngFor="let boletim of item">), and item ends up being null (from the backend's empty case), you'll get a runtime error—but again, not a syntax parsing error. Still worth checking if your template has unclosed tags or invalid Angular syntax.
Fixes to Try
Step 1: Verify Your API Response
Open your browser's DevTools (Network tab), trigger the ListaBoletim request, and check the Response tab:
- If you see HTML (like an error page), that's the root cause.
- If you see
nullor a valid array of boletim objects, move to the next steps.
Step 2: Fix Backend Exception Handling
Replace the generic throw new Exception(); with proper HTTP error responses so your frontend gets valid JSON:
[Route("ListaBoletim")] [HttpGet] public IActionResult ListaBoletim() { try { var listaBoletim = bBoletim.ListAll(); return Ok(listaBoletim.Count > 0 ? listaBoletim : new List<Boletim>()); } catch (Exception ex) { // Log the exception internally first return StatusCode(StatusCodes.Status500InternalServerError, new { message = "Failed to retrieve boletins" }); } }
Note: I also changed the empty case to return an empty array instead of null—this aligns better with your frontend's Boletim[] type.
Step 3: Safeguard Frontend Data Assignment
Even if the backend returns null, ensure your frontend always gets an array:
fnListar() { this.dbService.get('Boletim/ListaBoletim').subscribe(result => { this.item = result || []; // Fallback to empty array if result is null }, error => { // Handle API errors here (e.g., show a toast, log to console) console.error('Failed to load boletins:', error); this.item = []; }); }
Step 4: Align Frontend/Backend Types (Best Practice)
While not the cause of your current error, fixing the type mismatch will prevent future issues. Update your frontend Boletim class to match the backend's numeric types:
export class Boletim { nuMes: number = 0; nuAno: number = 0; dsTitulo: string = ''; dsDetalhes: string = ''; urlImagem: string = ''; urlPdf: string = ''; }
内容的提问来源于stack exchange,提问作者Leonardo Lima

