如何在本地主机服务器上使用javascript ajax调用java类函数

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

本文关键字:javascript ajax 调用 类函数 java 服务器 主机服务 主机      更新时间:2023-09-26

我得到了这样的东西:

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
}
}

我得到了Validate

函数现在我得到了一个HTML页面,看起来像这样:
<!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>

最后,我有一个文本字段,里面有一个硬编码的数字。

在按钮上点击,我需要这个数字,并将其发送到我的验证函数。

一旦函数完成,我需要结果写在按钮.....下面这里…

<p id="res">Result: </p>

必须是Result:true或Result:false

你可以这样做(确保你包含jQuery库!):

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                            
               }
    });
}

你可以在这个链接下找到你问题的答案:answer

阅读更多关于使用javascript使用rest服务的信息

下面是正确的代码…感谢Mechkov

function myFunction() {
console.log($);
        var str = txtCC1.value;
         $.ajax({
            type: 'GET',
            url: 'http://localhost:8080/CreditCardWEB/rest/cc/validate/'  +str,                 
            success: function(data) {                               
                     document.getElementById("res").innerHTML = "Result: " + data;         
            },
            error: function(jqXHR, textStatus, errorThrown) {
                console.log(jqXHR +' : '+ textStatus +' : '+ errorThrown);             
            }
        });                 
}