通过ajax检索Json文本时出现问题

通过ajax检索Json文本时出现问题,ajax,json,Ajax,Json,我无法通过servlet响应获取Json文本。servlet代码正在运行。我的Ajax代码有错误。代码 var json = eval('(' + xmlhttp.responseText +')'); …不归还任何东西。是否需要任何jar来执行此操作?下面是我的代码: //Servlet public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, Servl

我无法通过servlet响应获取Json文本。servlet代码正在运行。我的Ajax代码有错误。代码

var json = eval('(' + xmlhttp.responseText +')');
…不归还任何东西。是否需要任何jar来执行此操作?下面是我的代码:

//Servlet

public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    List result = new ArrayList();
    result.add(new SearchResponse("001", "User Manual", "Operator"));
    response.setContentType("application/json");         
    response.setCharacterEncoding("UTF-8");        
    response.getWriter().write(new Gson().toJson(result));
}
在我的Ajax中,我正在编写以下代码来获得它

function ajaxFunction() {
    alert("function called...");
    if (xmlhttp) {
        alert(AJAX_SERVLET);
        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);
            alert(json);
            var json = eval('(' + xmlhttp.responseText +')');
            request.setAttribute("output",json);
        } else {
            alert("Error during AJAX call. Please try again");
        }
    }
}

我无法帮助您使用JSP,但希望此示例对JavaScript部分有所帮助:

<script>
...
function handleServerResponse() {
    if (xmlhttp.readyState == 4) {
        if (xmlhttp.status == 200) {
            var json = JSON.parse(xmlhttp.responseText);

            // 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>
...
<h4>Product</h4>
<div>Number: <span id="product-number"></span></div>
<div>Type: <span id="product-type"></span></div>
<div>Function: <span id="func-designation"></span></div>
...

...
函数handleServerResponse(){
if(xmlhttp.readyState==4){
if(xmlhttp.status==200){
var json=json.parse(xmlhttp.responseText);
//json现在包含一个对象数组,例如:
//
// [
//   {
//“产品编号”:“001”,
//“产品类型”:“用户手册”,
//“指定”:“操作员”
//   }
// ]
//抓取数组中的第一个(也是唯一一个)对象
var firstRecord=json[0];
//通过选择DOM元素并重写其内容来更新UI
document.getElementById('product-number').innerHTML=
firstRecord.productNumber;
document.getElementById('product-type').innerHTML=
firstRecord.productType;
document.getElementById('func-designation').innerHTML=
firstRecord.com名称;
}否则{
警报(“AJAX调用期间出错,请重试”);
}
}
}
...
产品
编号:
类型:
功能:
...

PS——您可能需要考虑jQuery、MOOTooS或另一个现代JavaScript框架;它们使AJAX调用和使用DOM变得更加容易。

我也遇到了同样的问题,但我改变了初始化部分&在我的例子中,它可以工作

阻止代码:

     var json = eval('(' + xmlhttp.responseText +')');   
工作代码:

var json = "";  
     json = eval('(' + xmlhttp.responseText +')');     
问候
Shyam Miyatra

如果只使用
eval(xmlhttp.responseText)
甚至完全跳过eval部分,会发生什么?你有什么数据吗?你能把你的
回复文本
贴出来让我们看看它是否有效吗?哪种警报(如果有)会触发?您在哪种浏览器中测试?尽管JSON.parse在现代浏览器中几乎是标准的,但对于较旧的浏览器,您可能需要通过添加它。