Ajax读取后台返回值respnseText为空,该怎么处理
Ajax读取后台返回值respnseText为空
大家好,最近在做一个项目,需要简单应用Ajax。前台用的是Jsp,javascript,后台用的是SSH。
我从前台通过Ajax向后台传值时,后台可以得到传输的值,但是当后台向前台写值时,前台在读取responseText时,总是读取为空。并且xmlHttp.status返回为0. 但是我后台明明是写了数据的。IE和Chrome都试过了,不行
前台代码 right.jsp中js代码
大家好,最近在做一个项目,需要简单应用Ajax。前台用的是Jsp,javascript,后台用的是SSH。
我从前台通过Ajax向后台传值时,后台可以得到传输的值,但是当后台向前台写值时,前台在读取responseText时,总是读取为空。并且xmlHttp.status返回为0. 但是我后台明明是写了数据的。IE和Chrome都试过了,不行
前台代码 right.jsp中js代码
var xmlHttp=false;
function sendMessage()
{
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(E)
{
xmlHttp = false;
}
}
if (!xmlHttp && typeof XMLHttpRequest!='undefined'){
xmlHttp = new XMLHttpRequest();
}
if(xmlHttp)
{
// do something
var url="/OverTime/addOvertimeTemp.action";
//data to post
var workerids="";
var isProductAbout=0;
var chxs=document.getElementsByName("chkbox");
for(var i=0;i<chxs.length;i++){
if(chxs[i].checked){
workerids+=chxs[i].value;
if(i<chxs.length-1)
{
workerids+=",";
}
if(chxs[i].title=="3")
{
isProductAbout=1;
}
}
}
var reason=document.getElementById("addOtptempVo.reason").value;
//var workfanbu=0;
var worktypeid=document.getElementById("addOtptempVo.worktypeid").value;
var worklen=document.getElementById("addOtptempVo.worklen").value;
var workshift=document.getElementById("addOtptempVo.worklen").value;
if(worktypeid=="4")
{
worklen=0;
}
else
{
workshift=0;
}
var postinfo="";//construted by the values mentioned before.
//为postinfo添加VO的各个属性键值对
postinfo+="addOtptempVo.stfIds="+ workerids;
postinfo+="&addOtptempVo.reason="+reason;
postinfo+="&addOtptempVo.isProductabout="+isProductAbout;
postinfo+="&addOtptempVo.worktypeid="+worktypeid;
postinfo+="&addOtptempVo.worklen="+worklen;
postinfo+="&addOtptempVo.workshift="+workshift;
xmlHttp.open("POST",url,false);//原来是true,网上看到一种解决方案,改为false,仍然不行。
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send(postinfo);
//alert(workerids);
alert("addOtptempVo.stfIds="+ workerids+" "+"&addOtptempVo.reason="+reason+" "+"&addOtptempVo.isProductabout="+isProductAbout+" "+"&addOtptempVo.worktypeid="+worktypeid+" "+"&addOtptempVo.worklen="+worklen+" "+"&addOtptempVo.workshift="+workshift);
xmlHttp.onreadystatechange=getMessage;
}
else
{
alert("xmlHttp无法创建!");
}
}
function getMessage()
{
alert(xmlHttp.statusText);
alert("getmessage:xmlHttp.readyState:"+xmlHttp.readyState+";xmlHttp.Status"+xmlHttp.status);
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200||xmlHttp.status==0)