javascriptajaxdebuggingjsp

Send value from JavaScript file to Jsp file using Ajax(Proper way to debug)


Let me assume someone is new to programming the person took a lecture in programming but not sure of other knowledge.

E.G)) Below is the question the begginer might ask.

Here is my JavaScript file where my goal is to send value of variable va to jsp file and output it.
The problem is that I can't find the bug.

JavaScript file
$(function(){
  $("td").click(function() {
    var date     = $(this).html();
    var message  = prompt(year+"year "+month+"month "+ date+"day","null!");
    this.append(message);   
    var variable = "mememem";
    var sendData = new XMLHttpRequest();
    sendData.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
        alert("connected to server");
      }
    };

    sendData.open('GET','dataFile.jsp?na='+variable,true);
    sendData.send(null);
    window.location = "dataFile.jsp"; 
  })
});
JSP file
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
    <title>Insert title here</title>
  </head>
  <body>
<%
  String name = request.getParameter("na"); 
  out.print(name);
%>
</body>
</html>

Solution

  • I think the problem is you're sending variable instead of va.

    This line:

    sendData.open('GET','dataFile.jsp?na='+variable,true);
    

    Hope that is the root cause of your problem.