struts2 + jsonInsert title here function getAjaxObject(){ var xmlhttp=null; if(window.XMLHttpRequest){ xmlhttp=new XMLHttpRequest(); }else{ xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } return xmlhttp; } function tt(){ var us

来源:学生作业帮助网 编辑:六六作业网 时间:2024/11/24 04:29:07
struts2+jsonInserttitleherefunctiongetAjaxObject(){varxmlhttp=null;if(window.XMLHttpRequest){xmlhttp

struts2 + jsonInsert title here function getAjaxObject(){ var xmlhttp=null; if(window.XMLHttpRequest){ xmlhttp=new XMLHttpRequest(); }else{ xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } return xmlhttp; } function tt(){ var us
struts2 + json




Insert title here


function getAjaxObject(){
var xmlhttp=null;
if(window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}else{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
return xmlhttp;
}

function tt(){
var userid=document.getElementById("userid").value;
var password=document.getElementById("password").value;
var url="./test?userid="+userid+"&password="+password;
var xmlhttp=getAjaxObject();
xmlhttp.open("GET",url,true);
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
xmlhttp.send(null);
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
document.write(xmlhttp.responseText)
}
};
}









==
public String execute(){
String str="{\"userid\":\""+userid+"\",\"password\":\""+password+"\"}";
result=str;
return SUCCESS;
}

怎么得到的结果是:"{\"userid\":\"123456\",\"password\":\"123456\"}"
会是带引号和转义字符的?

struts2 + jsonInsert title here function getAjaxObject(){ var xmlhttp=null; if(window.XMLHttpRequest){ xmlhttp=new XMLHttpRequest(); }else{ xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } return xmlhttp; } function tt(){ var us
简单一点的方法就是不通过struts的返回类型,直接用response写就可以,代码如下:
HttpServletResponse response=ServletActionContext.getResponse();
response.setContentType("text/html;charset=utf-8");
//response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
String jsonString="{\"user\":{\"id\":\"123\",\"name\":\"张三\",\"say\":\"Hello , i am a action to print a json!\",\"password\":\"JSON\"},\"success\":true}";
out.println(jsonString);
out.flush();
out.close();
打字不易,如满意,望采纳.