[SOLVED] Calling $.get() gives XML error message and I am not using XML

[SOLVED] Calling $.get() gives XML error message and I am not using XML


I am learning and experimenting with AJAX and $.get() in jquery. I have adapted a test case from  tutorialspoiknt.com and changed the test case from using PHP to using JSP. I am running the test case using Firefox 61.0.2, Tomcat 8 and jquery-3.3.1.js.

I'm calling $.get() as follows:

  1. console.log("xxxxxxxxxxxx get");
  2. $.get("result.jsp",{ name: "Jim" },
  3.       function(data) {
  4.           $('#stage').html(data);
  5.       }
  6. );
  7. console.log("yyyyyyyyyyyy get");

The script executes and I get the log messages: 'xxxxxxxxxxx get' and 'yyyyyyyyyyyy get'. When I click on the "Load Data" button (not shown here, but in my code below), I get the following error messages in the console:

  1. XML Parsing Error: not well-formed Location:
  2. file:///home/jja/prog/java/jsp3rdEd/jjaora/jja_examples/result.jsp?name=Jim Line Number 2, Column 2: result.jsp :2:2
  3. XML Parsing Error: not well-formed Location:
  4. file:///home/jja/prog/java/jsp3rdEd/jjaora/jja_examples/getCallback.jsp Line Number 2, Column 2: getCallback.jsp :2:2
These error messages are specific to Firefox. When I run the same code in Chrome, I get different results, but I will leave that for a separate thread.

I found the following at   Stackoverflow:


the request header is not the question. The question is what the response header for Content-Type is. And your server is not sending one. So XHR falls back to assuming it's XML unless told otherwise via overrideMimeType. – Boris Zbarsky Oct 6 '11 at 4:02


This looks like it may be the problem that I am facing. The question then is how do I call the 'overrideMimeType' method, hopefully in a browser independent way? I searched for help on this and found a few topics in other forums, but so far I have not found a solution that I want to implement. The solutions seem to be platform dependent or tend to lead down a dark alley.

Can anyone suggest a alternative solution? My preferred solution is to set the Tomcat default to accept "text/html", not XML. I'm not sure if I can do this, but I will be investigating further.

Jim A.



Source code 'getCallback.jsp':

  1. <!DOCTYPE html>
  2. <html lang="en">
  3.     <head>
  4.         <meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta>
  5.         <title>The Callback Example</title>
  6.         <script type = "text/javascript" src="./MM_JAVASCRIPT2E/_js/jquery-3.3.1.js"></script>
  7.     
  8.         <script type = "text/javascript" language = "javascript">
  9.             // Adapted from https://www.tutorialspoint.com/jquery/ajax-jquery-get.htm
  10.             $(document).ready(function() {
  11.                 console.log("In getCallback.jsp 'ready' function");
  12.                 $("#driver").click(function(event){
  13.                     console.log("xxxxxxxxxxxx get");
  14.                     $.get("result.jsp",{ name: "Jim" },
  15.                         function(data) {
  16.                             $('#stage').html(data);
  17.                         }
  18.                     );
  19.                     console.log("yyyyyyyyyyyy get");
  20.                     return false;
  21.                 });
  22.                 console.log("Leaving getCallback.jsp 'ready' function");
  23.             });
  24.         </script>   
  25.     </head>
  26.         
  27.     <body>
  28.         <p>Click on the button to load result.jsp file −</p>
  29.         <span id = "stage" style = "background-color:#cc0;">
  30.             STAGE
  31.         </span>
  32.         <div><input type = "button" id = "driver" value = "Load Data" /></div>
  33.     </body>
  34. </html>

Source code "result.jsp":

  1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

  2. <%
  3.     <hr></hr>
  4.     String name = request.getParameter("name");
  5.     out.println("Name is: " + name);
  6.     <hr></hr>
  7. %>