gpt4 book ai didi

java - XMLHttpRequest() 是未定义的 Servlet

转载 作者:行者123 更新时间:2023-12-01 15:22:46 25 4
gpt4 key购买 nike

您好,我收到了这个脚本,但我不知道为什么我总是收到 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 始终为空;/,不知道为什么,如果您查看 validate 方法,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";
}
}
}

最佳答案

而不是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();

关于java - XMLHttpRequest() 是未定义的 Servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10645858/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com