简体   繁体   中英

XML Parsing Error: no root element found Location in Console FF

I'm use ASP.NET MVC end I take this error only in FF. Why I take this error message? What is the cause of this? I don't understand where the source of this error. Anyone have any thoughts?

在此处输入图像描述

Next Error: 在此处输入图像描述

Check this link for more information

Based on my research, the error message is only generated by FireFox when the render page is blank in Internet. For some reason, .NET generates a response type of "application/xml" when it creates an empty page. Firefox parses the file as XML and finding no root element, spits out the error message.

in other words, this is a known Firefox issue

Hoping this might help someone.... I changed the type of return value in Action method and it worked. More details can be found in this article .

Changed from

return Ok();

to

return Json("Ok");
public IActionResult MethodName()
{
    // YOUR CODE 
    return StatusCode(204);
}

Code 204 -The server successfully processed the request, and is not returning any content

For more details about status code check this page: List of HTTP status codes

I fixed this in my app by setting the Content-Type header on the server to text/plain . Like this in Node:

res.setHeader('Content-Type', 'text/plain');

Use whatever MIME type is appropriate for your data, ie application/json , etc.

We were getting this error when POSTing an axios call. And our test server was not in the

allowed-origin: 

list of hosts. Adding our server to allowed-origin in the application.yml resolved the issue.

This can happen too, if you have a [ValidateAntiForgeryToken] attribute on your controller action and try to submit the form without an actual token. You can render the token explicitly with

@Html.AntiForgeryToken()

which is the preferred solution or remove the validation attribute.

I've seen the XML Parsing error: no root element error in a few different situations. It's typically present in the Firefox developer console only.

  1. Returning a 200 OK response code with an empty body. Your server should instead return a 204 No Content response code.

  2. Returning a 204 No Content response code and then attaching content to the response. Your server should instead return a 200 OK response code.

  3. Returning an incorrect Content-Type . Set it to the same type that's used in your response body (JSON, XML, plaintext, etc.), or it could get parsed incorrectly.

If none of these fixes your issue, I would review all of your response headers for correctness. Other fields like Content-Length could presumable interfere as well.

This is the top Google result for this error message and I don't see an answer covering another tricky case yet, so here goes:

This will also happen if your SVG is served correctly, but is missing the closing </svg> tag! For example, in case the response is only served partially, because it is a streaming response and it errors out mid-way…

I resolved my problem with

return NoContent();

It seems more clear When you want to return nothing.

I fixed by returning OkObjectResult instead of Ok().

Old Code:

Return Ok();

New Code:

return new OkObjectResult(true);

Note: OkObjectResult can be used after adding namespace Microsoft.AspNetCore.Mvc.Infrastructure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM