[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:
- console.log("xxxxxxxxxxxx get");
- $.get("result.jsp",{ name: "Jim" },
- function(data) {
- $('#stage').html(data);
- }
- );
- 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:
- XML Parsing Error: not well-formed Location:
- file:///home/jja/prog/java/jsp3rdEd/jjaora/jja_examples/result.jsp?name=Jim Line Number 2, Column 2: result.jsp :2:2
- XML Parsing Error: not well-formed Location:
- 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.
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':
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta>
- <title>The Callback Example</title>
- <script type = "text/javascript" src="./MM_JAVASCRIPT2E/_js/jquery-3.3.1.js"></script>
-
- <script type = "text/javascript" language = "javascript">
- // Adapted from https://www.tutorialspoint.com/jquery/ajax-jquery-get.htm
- $(document).ready(function() {
- console.log("In getCallback.jsp 'ready' function");
- $("#driver").click(function(event){
- console.log("xxxxxxxxxxxx get");
- $.get("result.jsp",{ name: "Jim" },
- function(data) {
- $('#stage').html(data);
- }
- );
- console.log("yyyyyyyyyyyy get");
- return false;
- });
- console.log("Leaving getCallback.jsp 'ready' function");
- });
- </script>
- </head>
-
- <body>
- <p>Click on the button to load result.jsp file −</p>
- <span id = "stage" style = "background-color:#cc0;">
- STAGE
- </span>
- <div><input type = "button" id = "driver" value = "Load Data" /></div>
- </body>
- </html>
Source code "result.jsp":
- <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
-
- <%
- <hr></hr>
- String name = request.getParameter("name");
- out.println("Name is: " + name);
- <hr></hr>
- %>