添加json2.js时出现语法错误

问题描述

| 我想在jsp页面显示来自servlet的json响应。下面是代码。但是,当我包含json2.js时,浏览器会报告语法错误。我正在Liferay门户网站上工作。请建议我是否做错了什么。
<html>
<head><script type=\"text/javascript\"> var AJAX_SERVLET=\"<%=renderResponse.encodeURL(renderRequest.getcontextpath())%>/ajaxServlet\";
</script>               
<script type=\"text/javascript\" src=\"json2.js\">
/**
 * This file contains the base Ajax call to Servlet
 */
function getXMLObject() //XML OBJECT
{
    //alert(\"loading...\");
    var xmlHttp = false;
    try {

        xmlHttp = new ActiveXObject(\"Msxml2.XMLHTTP\") // For Old Microsoft browsers
    } catch (e) {
        try {

            xmlHttp = new ActiveXObject(\"Microsoft.XMLHTTP\") // For Microsoft IE 6.0+
        } catch (e2) {
            xmlHttp = false // No browser accepts the XMLHTTP Object then false
        }
    }
    if (!xmlHttp && typeof XMLHttpRequest != \'undefined\') {

        xmlHttp = new XMLHttpRequest(); //For Mozilla,Opera browsers
    }

    return xmlHttp; // Mandatory Statement returning the ajax object created
}

var xmlhttp = new getXMLObject(); //xmlhttp holds the ajax object

function ajaxFunction() {
    if (xmlhttp) {
        xmlhttp.open(\"GET\",AJAX_SERVLET,true); //AJAX_SERVLET has the servlet path
        xmlhttp.onreadystatechange = handleServerResponse;
        //xmlhttp.setRequestHeader(\'Content-Type\',//\'application/x-www-form-urlencoded\');
        xmlhttp.send(null);
    }
}
function handleServerResponse() {
alert(xmlhttp.readyState);
    if (xmlhttp.readyState == 4) {
        alert(xmlhttp.status);
        if (xmlhttp.status == 200) {
        alert(xmlhttp.responseText);
            var json = JSON.parse(xmlhttp.responseText);
            alert (json);
            // json Now contains an array of objects,eg:            
            //            
            // [            
            //   {            
            //     \"productNumber\"  : \"001\",//     \"productType\"    : \"User Manual\",//     \"funcDesignation\": \"Operator\"            
            //   }            
            // ]    
            // grab the first (and only) object in the array             
            var firstRecord = json[0];
                // update the UI by selecting DOM elements and rewriting their contents          
            document.getElementById(\'product-number\').innerHTML = firstRecord.productNumber;            
            document.getElementById(\'product-type\').innerHTML =   firstRecord.productType;             
            document.getElementById(\'func-designation\').innerHTML = firstRecord.funcDesignation;            
            } else {
            alert(\"Error during AJAX call. Please try again\");
        }
    }
}</script>
 </head>
<body>
    <form name=\"myForm\">
        <h4>
            <b>Hello</b>,</h4>
        <h5><%=renderRequest.getAttribute(\"userName\")%></h5>
        <div id=\"maincontent\">
            <div class=\"innertube\">
                <h4>Search Criteria</h4>
                <table style=\"border: 1px solid #9f9f9f; float: left\"
                    cellspacing=\"1\">
        <table style=\"border: 1px solid #9f9f9f; float: right;\">

<tr><td><label for=\"status\">Search Status</label></td>
<td><input type=\"text\" id=\"status\" name=\"status\" dojoType=\"dijit.form.TextBox\" size=\"40\"
value=\"Please enter search criteria\" /></td>
</tr>
<tr>
<td><label for=\"push\">Push to start</label></td>
<td><button dojoType=\"dijit.form.Button\" style=\"width: 4em\" type=\"button\" name=\"submitButton\" value=\"Submit\" onclick=\"ajaxFunction()\"></button></td>
</tr>
</table>
</div>
</div>
<div id=\"framecontent\">
<div class=\"innertube\">
    <div>Number: <span id=\"product-number\"></span></div> 
    <div>Type: <span id=\"product-type\"></span></div> 
    <div>Function: <span id=\"func-designation\"></span></div>
    </div>
    </div>
    </form>
</body>
</html>
    

解决方法

        好像/json2.js指向非JavaScript文件。您可以验证此文件是否出现在服务器上,并且/json2.js解析为所需的文件吗?否则,请使用JSON解析器实现的绝对路径:
<script type=\"text/javascript\" src=\"http://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js\"></script>
    ,        您可以使用
<script>
标记在页面中包含脚本或加载外部文件。所以:
<script type=\"text/javascript\" src=\"json2.js\" />
<script type=\"text/javascript\">
    /**
    * This file contains the base Ajax call to Servlet
    */
    function getXMLObject() //XML OBJECT
    {}
</script>
    

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...