14

I am new to developing websites. I know that I have to map servlets in the web.xml file. The web.xml file is this

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
    <display-name>TestApp</display-name>
    <welcome-file-list>
     <welcome-file>index.html</welcome-file>
     <welcome-file>index.htm</welcome-file>
     <welcome-file>index.jsp</welcome-file>
     <welcome-file>default.html</welcome-file>
     <welcome-file>default.htm</welcome-file>
     <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
     <servlet-name>Serve</servlet-name>
     <servlet-class>Serve</servlet-class>
    </servlet>
    <servlet-mapping>
     <servlet-name>Serve</servlet-name>
     <url-pattern>/TestApp</url-pattern>
    </servlet-mapping>
   </web-app>

But when I call the jquery $.ajax() function, I get this error.

XML Parsing Error: syntax error Location: http://localhost:8080/TestApp/Serve Line Number 1, Column 1:

The AJAX Call is

    $.ajax({
    url: "Serve",
    type: "POST",
    success: function(out){
        alert(out);
    },
    error: function(){
        alert("No");
    }
});

The problem is the servlet still runs fine. The alert(out) works as expected. Please explain why the browser is showing the error and please tell me a solution.

If this is a duplicate question please give a link to the original question.

Robbie
  • 143
  • 1
  • 1
  • 8
  • May be you are running your app in `file:///`, since this is ajax request this can be happen. If you have installed nodejs, you can install a server and see console log – Blasanka Nov 28 '18 at 16:06

3 Answers3

33

I recently encountered the same issue. jQuery appeared to be handling the data and the dataType correctly, but instead it was Firefox returning the syntax error, which explains why your code was executing as intended but still printing an error to the console.

If you look in the developer console, you can see that Firefox is interpreting the plain text data as another format (likely XML). Firefox tires to parse the data as XML, but can't because it's not valid XML which results in "Syntax error" being printed to the console.

Fixing this problem for me involved editing the server so it returned the following header:

Content-Type: "text/plain"

This only appeared to be an issue with Firefox, Chrome did not encounter this issue. There is a Firefox bug here which seems to touch on the issue.

source

Daniel Gray
  • 1,539
  • 1
  • 19
  • 39
Cholowao
  • 887
  • 12
  • 18
7

The problem still exists in Firefox 70, at least when requesting a file from the file system. No jquery needed, the behavior can be reproduced with a plain XMLHttpRequest. Calling its overrideMimeType method before send solved it for me. Looks like a quite clean solution to me. Example:

var xhr = new XMLHttpRequest();
xhr.open("GET", window.location, true);
xhr.overrideMimeType("text/html");
xhr.onreadystatechange = function()
{
    if (xhr.readyState == 4) alert(xhr.responseText);
}
xhr.send();
tglas
  • 881
  • 10
  • 18
  • Brilliant work! Well done and thank you. I have been trying to retrieve (via XHR) an `.mjs` file and Firefox has, inexplicably, been returning: `XML Parsing error: syntax error`. This line: `xhr.overrideMimeType('application/javascript')` solved the issue. – Rounin - Standing with Ukraine Jan 01 '20 at 16:55
  • Can confirm, this is still occurring on Firefox v77.0.1. – dsomel21 Jun 14 '20 at 20:49
  • For me, `responseType` works as well. I set it to `"text"` and now I don't have to fiddle with mime types. – aleskva Nov 21 '21 at 00:35
  • Thus It might be an issue in Firefox's handling of response type as mime type depends on the response type. – aleskva Nov 21 '21 at 00:47
0

So to specifically answer OP, the code below should make the error go away.

(replace the 'application/xml' with whatever MIME Type is desired. For example, 'application/json' is the most common.)

    $.ajax({
    url: "Serve",
    type: "POST",
    beforeSend: function (xhr) {
       xhr.overrideMimeType('application/xml');  // this line prevents XML parsing error with firefox
    },
    success: function(out){
        alert(out);
    },
    error: function(){
        alert("No");
    }
});
BassGod
  • 71
  • 8