1

I am attempting to set up a function that a button will call and activate an ajax command to send a variable called [finalstring] to a servlet, while simultaneously calling the servlet to open up.

This is the code.

function send(){
    var finalstring = newstrings.join(',');
    finalstring = finalstring.substring(0,finalstring.length -3);
    $.ajax({
        type: "post",
        url: "arraycomp",
        data: {"Value": finalstring},
        success: function (data) {
            if (data == "0"){
                alert("Input Submitted!");
            } else {
                alert("ERROR: Failed to submit Input.")
            }
        }
    });
}

However, the problem here is that 1. The data value is not being read at all, as the if and else statement I put in keeps reading false sending the alert that it was failed to submit input. 2. At the same time, the call doesn't seem to be working as the page is still in the same jsp.

So I am wondering if there is just something wrong with this function, because the rest of the codes are completely fine. Anyone who knows more about ajax can you please point out what I did wrong here?

  • Show your backend code as well. Also , problem is inside success function and what you mean by *simultaneously calling the servlet to open up..* ? – Swati Jan 26 '21 at 07:17
  • I'm trying to make it so that the ajax would send the variable to my servlet as a "Value" while also moving to the servlet like how a form's action would move to that specified file on 'submit'. The jsp code is a lot since I made numerous functions for if and nested if statements. I will try to limit to what is needed. – Ananimosity Jan 26 '21 at 07:43

1 Answers1

0

This is the 'form' in which I use to make the inputs.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>TextArea Input</title>
</head>
<body>

<form>
Input Area: Format for input = FirstName,LastName,8DigitIDNum (Comma included)
<br/>
<TEXTAREA id = "textarea" COLS = "40" ROWS = "10" onkeypress = "arraybuild(event, this)"></TEXTAREA>
<br/>
<br/>
<input type="button" id="submit" value="Submit" disabled = "true" onclick="send(this)"/>
<br/> <br/>
<input type="button" value="Reset" onclick = "unlock(this)"/>
<br/> <br/>
</form>

<script type="text/javascript">

This is the main code, its a function for a text area that makes an array input every time I press enter. The finalstring is supposed to be the array compressed into a string for me to send to the servlet.

var input;
var uneditstring;
var editremovespace;
var newstrings;

function arraybuild(e){
    var input = (e.keyCode ? e.keyCode : e.which);
    if (input == 13){//Note: key number 13 is Enter Key
        uneditstring = document.getElementById("textarea").value;
        editremovespace = uneditstring.split(" ").join("");
        newstrings = editremovespace.replace(/\n/g, ",").split(",");
        console.log(newstrings)
        if(newstrings.includes('-1')){
        newstrings.pop();
        console.log(newstrings)
            if(checkValues(newstrings)){
                if(checkID(newstrings)){
                    if(checkNames(newstrings)){
                        document.getElementById("textarea").readOnly = true;
                        document.getElementById("submit").disabled = false;
                        alert('You entered -1, you can now submit the inputted values.')
                        alert('If you want to reopen the text area, click Reset.')
                    }else{
                        alert('ERROR: Names are not valid.');
                        alert('ERROR: Due to containing a number or being over 255 characters.');
                    }
                }else{
                    alert('ERROR: One of the IDs is not Valid.');
                    alert('ERROR: Due to starting with a double zero, \nhaving a letter in them, \nor being longer than 8 digits.');
                }
            }else{
                alert('ERROR: Note enough values inputted. At least 3 Values are required.');
            }
        }else{
            console.log('Not yet pressed -1');
        }
    }
}

Neither of these codes have given me any errors at all. PS: Apologies if it is still too long.