未在服务器上检索JSON对象

时间:2015-11-28 11:10:16

标签: java jquery ajax servlets

我通过在ajax中对其进行字符串化来传递JSON对象。但它不是在服务器端检索的。

jsonData = {
    "10001":{
        "2":["33","","8","","9",""],
        "name":"abc",
        "token":"10001",
        "01":["","36","","1","1","8"]
    },
    "10002":{
        "2":["67","87","78","5","44",""],
        "name":"dfds",
        "token":"10002",
        "01":["9","","7","3","5","7"]
    }
}

使用ajax传递Object,如下所示:

var jsonData = JSON.stringify(emp);                   
$.ajax({
    url: "SaveController",
    type: "POST",
    dataType: "json",                  
    data: {empData:jsonData},
    contentType: "application/json; charset=utf-8",
    async: false,
    cache: false,                        
    success: function(response){
        alert("Data Saved...");
        alert(JSON.stringify(response));
    },
    error: function(err){
        alert("Error in Saving Data...");
        alert(JSON.stringify(err));
    }
});           

我正在我的SaveController中检索它,如下

response.setContentType("application/json");
PrintWriter out = response.getWriter();
try {
    String term = request.getParameter("empData");                      
    System.out.println( term);       
} finally {
    out.close();
}

已在客户端检查neturl数据在post中作为参数字符串发送,但未在服务器端使用getParameter检索。它返回null。

1 个答案:

答案 0 :(得分:0)

经过许多文章后,调整使其发挥作用 删除
contentType:" application / json;字符集= UTF-8",