我有一个大问题,我的代码中没有任何错误。该程序应从字段中添加两个值p1
并将p2
它们上传到站点,而无需重新加载整个页面。我是用简单的 javascript 完成的,但我想使用 AJAX。一切都已经完成,但某处存在小错误。
我看validate()
and insterter()
,警报具有适当的价值
alert("data = "+data);
req.send(data);
但是来自 insterter 的味精是空的。我不知道为什么。请帮忙。我想更正这段代码。
var msg = req.responseText;
alert("msg = "+msg);
为什么 msg 为空的原因是什么?
@WebServlet("/Serwlet")
public class Serwlet extends HttpServlet {
private static final long serialVersionUID = 1L;
static String KOD =
"<html>"+
"<head>"+
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1250\">"+
"<title>Testowanie</title>"+
"<script type=\"text/javascript\" language=\"javascript\" src=\"servletD2Script.js\"></script>"+
"</head>"+
"<body>"+
"<div id=\"msg1\">Pierwsza suma</div>"+
"<div id=\"msg2\">Pierwsza suma</div>"+
"<center><h2>Testowanie parametrów</h2></center>"+
"<hr>"+
"<form method=\"get\" action=\"Serwlet\">"+
"p1<input type=\"text\" size=\"50\" name=\"p1\" onblur=\"validate()\"><br>"+
"p2<input type=\"text\" size=\"50\" name=\"p2\" onblur=\"validate()\"><br>"+
"<br><input type=\"submit\" value=\"Wyślij formularz by GET\">"+
"</form>"+
"</hr>"+
"<hr>"+
"<form method=\"post\" action=\"Serwlet\">"+
"p3<input type=\"text\" size=\"50\" name=\"p3\" onblur=\"funkcja()\"><br>"+
"p4<input type=\"text\" size=\"50\" name=\"p4\" onblur=\"funkcja()\"><br>"+
"<br><input type=\"submit\" value=\"Wyślij formularz by POST\">"+
"</form>"+
"</hr>"+
"</body></html>";
public Serwlet() {
super();
}
static boolean raz = true;
private void serviceRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
response.setHeader("Cache-Control", "no-cache");
PrintWriter out = response.getWriter();
if(raz){out.print(Serwlet.KOD);raz = false;}
String name = request.getParameter("name"); // dostaję nazwisko
String org = ""; //jakas wartosc
out.write(org != null ? org : ""); // zwracam organizację lub pusty napis, jesli nie znam nazwiska
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
serviceRequest( request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
serviceRequest( request, response);
}
}
var req;
function validate() {
//var idField = document.getElementById("name");
var wrt1 = document.getElementById("p1").value;
var wrt2 = document.getElementById("p2").value;
var data = eval(wrt1)+eval(wrt2)+"";
if (typeof XMLHttpRequest != "undefined") {
req = new XMLHttpRequest();
} else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
}
var url = "Serwlet";
req.open("POST", url, true);
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
alert("data = "+data);
req.send(data);
req.onreadystatechange = inserter;
}
function inserter() {
if (req.readyState == 4) {
if (req.status == 200) {
var msg = req.responseText;
alert("msg = "+msg);
if (msg == "") {
document.getElementById("msg1").innerHTML = "<div style=\"color:red\">"+"MSG WAS EMPTY"+"</div>";
//document.getElementById("msg1").value = "blabla";
}
else
document.getElementById("msg1").innerHTML = "<div style=\"color:red\">"+msg+"</div>";
//document.getElementById("msg1").value = "COOOO JEST";
}
}
}
这部分代码总是被调用味精是“”
if (msg == "") {
document.getElementById("msg1").innerHTML = "<div style=\"color:red\">"+"MSG WAS EMPTY"+"</div>";
//document.getElementById("msg1").value = "blabla";
}