English 中文(简体)
jsp 通过jquery-jaax没有收到json [复制]
原标题:json not received in jsp through jquery-ajax [duplicate]

i am sending 2 data to the servlet through ajax from jsp and the servlet just get those data, then convert to json obj and return that json. now the jsp get that json to show those 2 data. ajax is successfully executing the servlet but everytime is alerts me the error not the success. pls tell me what should i do to display those data in alert? details.jsp:

<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>            
    function doajax(){
        $.ajax({
            url: "AccountDetails",
            type: "post",
            dataType: "json",
            data: { "mobileNo":"01914752849", "fullName":"Md. Muzahid-ul Islam" },
            error:function(){
                alert("error occured!!!");
            },
            success:function(data){
                alert(data.fullName + "
" + data.mobileNo);
            }
         });
      }
</script>

账户 Details.java( 服务器) :

PrintWriter out = response.getWriter();
try {
    String fullName = request.getParameter("fullName");
    String mobileNo = request.getParameter("mobileNo");
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("fullName", fullName);
    jsonObject.put("mobileNo", mobileNo);
    out.println(jsonObject);
} finally {
    out.flush();
    out.close();
}
问题回答

编辑服务器中的代码到

Gson gson = new Gson();
JsonElement element = gson.toJsonTree(<Object_to_be_passed or model_class>);
out.write(element.toString());

而且您还必须在服务器中导入两个包 com.google.gson.Gson com.google.gson.JsonEplement

注意: 尝试通过在模型类中包装数据来转发数据

希望它能有所帮助..) :)





相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

How can i update div continuously

I have asp.net application where i have a div which showing the value from other site. The value of that site is changing continuously. I want that my div will automatically update in some interval ...

热门标签