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

发布于 2024-11-15 21:02:21 字数 4287 浏览 0 评论 0原文

我想在 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>

I want to display json response from the servlet in jsp page. Below is the code. However when I include json2.js;the browser reports syntax error. I am working on liferay portal. Please suggest if I am doing anything wrong.

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奈何桥上唱咆哮 2024-11-22 21:02:21

似乎 /json2.js 指向一个非 JavaScript 文件。您可以验证该文件是否存在于您的服务器上,并且 /json2.js 解析为所需的文件吗?否则,使用 JSON 解析器实现的绝对路径:

<script type="text/javascript" src="http://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js"></script>

Seems like /json2.js points to a non-JavaScript file. Can you verify that this file presents on your server, and /json2.js resolved to the needed file? Otherwise, use absolute path to JSON parser implementation:

<script type="text/javascript" src="http://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js"></script>
胡渣熟男 2024-11-22 21:02:21

您可以使用

<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>

You either use the <script> tag to include a script in the page or to load an external file. So:

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文