jsp Ajax GET工作POST无法正常工作

时间:2013-05-13 07:19:20

标签: ajax jsp tomcat6

我需要刷新浏览器的某些部分。因此我使用的是Ajax。当我使用GET方法时,我可以在服务器上看到数据,但是当我使用POST时,我无法做到。 有什么指示吗?

----------- searchPan.jsp的代码-------------------------

<html>
  <%
  try
  {
          System.out.println("url is " + request.getRequestURI());
          System.out.println("pan is " + request.getQueryString());
          System.out.println("pan is " + request.getParameter("pan"));
  }
  catch(Exception E)
  {
          System.out.println("Error happened " + E);
  }
          //promo_card cardobj = new promo_card();
          //out.println(cardobj.getData());
  %>
</html>

---------------------------- GET代码---------------- --------------------

xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
 if(xmlhttp.readyState==0)
         document.getElementById('status').innerHTML='Uninitialized';
 else if(xmlhttp.readyState==1)
         document.getElementById('status').innerHTML='Loading...';
 else if(xmlhttp.readyState==2)
         document.getElementById('status').innerHTML='Loaded';
 else if(xmlhttp.readyState==3)
         document.getElementById('status').innerHTML='Interactive';
 else if(xmlhttp.readyState == 4 && xmlhttp.readyState == 200)
         document.getElementById("div1").innerHTML = xmlhttp.responseText;
}
if(xmlhttp)
{
  var queryString = 'searchPan.jsp?pan=' + document.getElementById('pan_number').value;
  xmlhttp.open("GET", queryString, true);
  xmlhttp.send();
}

当我使用GET方法时,我得到以下输出

url is /cardsit/promo/card/searchPan.jsp
pan is pan=123456789
pan is 123456789

When I use POST, I am unable, .. as below.
url is /cardsit/promo/card/searchPan.jsp
pan is null
pan is null

---------------------- POST代码---------------------- ------

xmlhttp.open("POST", "searchPan.jsp", true);
  var queryString = 'searchPan.jsp';
  queryString = 'pan=' + document.getElementById('pan_number').value;
  alert(queryString);
  xmlhttp.send(queryString);

2 个答案:

答案 0 :(得分:1)

添加这一行,它最后帮助了我。

xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

答案 1 :(得分:0)

尝试使用以下代码

xmlhttp.open("POST", "searchPan.jsp", true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
var queryString = 'pan=' + document.getElementById('pan_number').value;
xmlhttp.send(queryString);