ajax与servlet利用json传送数据

前端之家收集整理的这篇文章主要介绍了ajax与servlet利用json传送数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
js端
var param=“{'dn':'dn','area':'area','yoursugguest':'suggest',}";
param = eval('(' + param + ')');
$.ajax({
    url:"/liferayStudy-servlet/CreateTeamServlet",    async:false,timeout:'900000',type:'POST',    data: param,encode:'UTF-8',datatype:'jsonp',success:function(data,textStatus){	
        data= eval("(" + data + ")");  
//					parent.location.href="/livemeeting?"
//						+"teamId="+data["teamId"];
    //201207081712
        parent.window.location.href="../../en_GB/web/"+data["title"].toString()+"/home";
    },error:function(XMLHttpRequest,error){
        if (error == "timeout")
            alert("timeout");
        if (error == "parsererror")
            alert("parsererror!");
    }		
});

servlet端

JSONObject param = new  JSONObject();
JSONObject responseJSON = new JSONObject();
		
		
int teamID = -1;
try {
    param.put("team",request.getParameter("dn"));
    param.put("userId","1");
    param.put("title",request.getParameter("area"));
    param.put("des",request.getParameter("yoursuggest"));
			
    System.out.println("area =="+ request.getParameter("area"));
    CreateTeamUtil.addLiferaySite(param,request);
    CreateTeamUtil.addTeam(param);
    teamID= CreateTeamUtil.getTeamID(param);
} catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (sqlException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}
//		if(teamID != -1) {
try {
	//responseJSON.put("teamId",teamID);
	responseJSON.put("title",request.getParameter("area"));
} catch (JSONException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}
	System.out.println("----reponseJson--- " + responseJSON.toString());
	response.getWriter().print(responseJSON.toString()); //返回数据
原文链接:https://www.f2er.com/ajax/166110.html

猜你在找的Ajax相关文章