newbie newbie - 1 month ago 16
Ajax Question

How to call a java class function with javasrcipt ajax on a localhost server

i got something like this

package beans;
import javax.ejb.Stateless;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("cc")
@Stateless
public class CardBean implements ICardRemote {

@Produces(MediaType.APPLICATION_JSON)
@Path("validate/{creditCard}")
@GET
@Override
public boolean Validate(@PathParam("creditCard")String creditCard){
int sum = 0;
boolean alternate = false;
for (int i = creditCard.length() - 1; i >= 0; i--)
{
int n = Integer.parseInt(creditCard.substring(i, i + 1));
if (alternate)
{
n *= 2;
if (n > 9)
{
n = (n % 10) + 1;
}
}
sum += n;
alternate = !alternate;
}
return (sum % 10 == 0); //or true or false
}
}


there i got the function Validate

now i got a HTML page, that looks like this:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="code.jquery.com/jquery-3.1.1.min.js"; integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b‌​8=" crossorigin="anonymous"> </script>


</head>
<body>
<a
href="http://localhost:8080/CreditCardWEB/rest/cc/validate/4111111111111111">Validan</a>
<br>
<a
href="http://localhost:8080/CreditCardWEB/rest/cc/validate/4111111111111112">Nevalidan</a>
<br>

<br>
<input type="text" name="txtCC" value="4111111111111111" id="txtCC1">

<button name="btn" onclick="myFunction()">Click me!</button>
<br>
<br>
<p id="res">Result: </p>


<script>
function myFunction() {

// document.getElementById("res").innerHTML = "Result:" + txtCC1.value;
var str = txtCC1.value;

$.ajax({
type: 'GET',
url: './validate/'+str,
success: function(data) {
document.getElementById("res").innerHTML = "Result:" + data;
},
error: function(jqXHR, textStatus, errorThrown) {
//Do something on ERROR here
}
});
}
</script>

<br>

</body>
</html>


so in the end, i have a text field, with a hardcoded number in it...
on button click i need to take that number and send it into my validation function...

and once the function is done, i need the result to be written bellow the button.....here...
Result:
so it has to be like Result:true, or Result:false

And i have no idea how to do it....pls help

Answer

You can do something like this (make sure you include jQuery library!):

function myFunction() {
    document.getElementById("res").innerHTML = "Result:" + txtCC1.value;
    var str = txtCC1.value;

    //Here i need a code to call my validation function 
    //like: var res=Validation (str);
    //and then do the  
    //document.getElementById("res").innerHTML = "Result:" + res;
    //DONE


     $.ajax({
        type: 'GET',
        url: './validate/'+str,   //Make sure you put the correct endpoint URL here!                
        success: function(data) { 
                    //DO SOMETHING HERE AFTER YOU GET THE RESPONSE FROM the validate function
                    document.getElementById("res").innerHTML = "Result:" + data;
                 },
        error: function(jqXHR, textStatus, errorThrown) {
                    //Do something on ERROR here                            
               }
    });

}