您好我有这个脚本,我不知道为什么我总是得到req = XMLHTTPRequest是未定义的。 你能告诉我我犯了什么错误吗?
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);
// var data = "name=" + encodeURIComponent(idField.value);
req = new XMLHttpRequest();
// req = new XMLHttpRequest();
var url = "Serwlet";
req.open("POST", url, true);
req.onreadystatechange = inserter;
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
req.send(data);
}
function inserter() {
if (req.readyState == 4) {
if (req.status == 200) {
var msg = req.responseText
if (msg == "") {
//document.getElementById("msg1").innerHTML = "<div style=\"color:red\">"+"COS NIE TAK"+"</div>";
document.getElementById("msg1").value = '';
}
else
//document.getElementById("msg1").innerHTML = "<div style=\"color:red\">"+wynik+"</div>";
document.getElementById("msg1").value = "IT WORKS";
}
}
}
修改 的
好的,我做了一些改进。现在唯一的问题是来自JavaScript inserter()的msg总是空的; /,不知道为什么,如果你看一下验证方法var数据有正确的值,msg总是为null; /。
package Pakiet;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import javax.servlet.ServletException;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/Serwlet")
public class Serwlet extends HttpServlet {
private static final long serialVersionUID = 1L;
static String KOD =
/*
out.print("<FORM ACTION=\"ExerciseE\" name=\"entryForm\">\n"+
"Name:<INPUT TYPE=\"TEXT\" NAME=\"name\"><BR>\n"+
"<INPUT TYPE=\"BUTTON\" VALUE=\"SUBMIT\"\n" +
" OnClick=\"validate()\">\n</FORM>");
*/
"<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);
}
}
和脚本 //方法FUNCKJA()并非重要
function funkcja(){
var wrt1 = document.getElementById("p1").value;
var wrt2 = document.getElementById("p2").value;
var wynik = eval(wrt1)+eval(wrt2);
document.getElementById("msg1").innerHTML = "<div style=\"color:red\">"+wynik+"</div>";
var wrt3 = document.getElementById("p3").value;
var wrt4 = document.getElementById("p4").value;
var wynik2 = eval(wrt3)+eval(wrt4);
document.getElementById("msg2").innerHTML = "<div style=\"color:red\">"+wynik2+"</div>";
}
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\">"+"COS NIE TAK"+"</div>";
//document.getElementById("msg1").value = "blabla";
}
else
document.getElementById("msg1").innerHTML = "<div style=\"color:red\">"+msg+"</div>";
//document.getElementById("msg1").value = "COOOO JEST";
}
}
}
答案 0 :(得分:1)
而不是req = new XMLHttpRequest
试试这个:
if(window.XMLHttpRequest && !(window.ActiveXObject)) {
try {
req = new XMLHttpRequest();
} catch(e) {
req = false;
}
} else if(window.ActiveXObject) {
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {
req = false;
}
}
}
偏离主题:如果ajax是您的要求,您应该考虑使用某些库,例如: jQuery的。
编辑:
根据评论:
if(raz){out.print(Serwlet.KOD);raz = false;}
你能检查raz是否真实吗?
然后写入流程调用out.flush();
if(raz){
out.print(Serwlet.KOD);
raz = false;
}
String name = request.getParameter("name"); // dostaję nazwisko
String org = ""; //jakas wartosc
out.write(org != null ? org : "");
out.flush();