javascriptintellij-ideathymeleafsaxparserisbn

SAX parse error while using ISBN


I am using thymeleaf and I am using a function which gets the ISBN number and fetch book data. I tried the code in an online editor and its working fine but when I use the same code in Intellij

org.xml.sax.SAXParseException: The entity name must immediately follow the '&' in the entity reference. at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203) ~[na:1.8.0_101] at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:177) ~[na:1.8.0_101] at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:400) ~[na:1.8.0_101] at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:327) ~[na:1.8.0_101]

Here's my code HTML

<div class="input-field col s6 m6">
        <input id="isbn" name="isbn" type="text" class="validate" />
        <label for = "isbn">Enter ISBN Code</label>
</div>
    <div class="input-field col s6 m6">
        <button id="submitCode" class="btn waves-effect waves-light col m4" onclick="myFunction()" value="data">ISBN Data</button>
    </div>

JavaScript Code

function myFunction()
{
    var isbn = document.getElementById('isbn').value;
    alert(isbn);
    var xmlhttp = new XMLHttpRequest();
    var url = "https://www.googleapis.com/books/v1/volumes?q=isbn:"+isbn;
    xmlhttp.onreadystatechange = function()
    {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
        {
            var x = JSON.parse(xmlhttp.responseText);
            callback(x);
        }
    };
    xmlhttp.open("GET", url, true);
    xmlhttp.send();
}
function callback(x)
{
    //do things with your data here
    alert(JSON.stringify(x));
    console.log(x);
}

Solution

  • Thymeleaf requires valid XML, and in your javascript you have:

    if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
    

    The & isn't a valid XML character, so you need to either surround your javascript with CDATA like this:

    <script>
    // <![CDATA[
    
    ... javascript here ...    
    
    // ]]>
    </script>
    

    Or else replace them with &amp;&amp;