简体   繁体   English

通过从输入文本框中传递参数,通过jQuery AJAX调用Java EE REST服务

[英]Calling Java EE REST service through jQuery AJAX by passing the parameters from input text box

I have an html code to create an input text box that accepts the input form user.And the parameters must be passed along with url to rest service. 我有一个html代码来创建一个接受输入表单用户的输入文本框。参数必须与url一起传递给rest服务。 This is my ajax call code: 这是我的ajax调用代码:

$(function() {

var empid = document.getElementById("ManagerId").value;
$('#submit').click(function(){ 
$.ajax({ 
    crossDomain : true,
     type: "GET",
     dataType: "json",
    url: "http://localhost:8088/JirasTrackingApp/reporter/Reportees?empid="+empid,

     success: function(result){        
        console.log(result);
        document.write(empid.value);
     }
 });
});

This is my Service: 这是我的服务:

@Path("/Reportees")
public class ReporteesService {
    ReporteeList   reportee = new ReporteeList();

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Map<Object, Object> getList(String empid) throws Exception {
        System.out.println("id is"+empid);  //when I try to print the empid,it  displays nothing
        Map<Object, Object> map=reportee.getReportees(empid);  
        return map;             
    }
});

This is my getReportees() in ReporteeList class 这是我在ReporteeList类中的getReportees()

public class ReporteeList {

    public Map<Object, Object> getReportees(String idOfEmp) throws Exception {
        System.out.println(idOfEmp);
        String msg = "error";
        String api = "https://connect.ucern.com/api/core/v3/people/";
        String id = idOfEmp;
        String ext = "/@reports";
        String url = api + id + ext;
        String name = "*********";
        String password = "*********";
        String authString = name + ":" + password;
        String authStringEnc = new BASE64Encoder().encode(authString.getBytes());
        System.out.println("Base64 encoded auth string: " + authStringEnc);
        Client restClient = Client.create();
        WebResource webResource = restClient.resource(url);
        ClientResponse resp = webResource.accept("application/json")
                                         .header("Authorization", "Basic " + authStringEnc)
                                         .get(ClientResponse.class);
        if (resp.getStatus() != 200) {
            System.err.println("Unable to connect to the server");
        }
        String output = resp.getEntity(String.class);

        // JSONParser reads the data from string object and break each data into key
        // value pairs
        JSONParser parse = new JSONParser();
        // Type caste the parsed json data in json object
        JSONObject jobj = (JSONObject) parse.parse(output);
        // Store the JSON object in JSON array as objects (For level 1 array element i.e list)

        JSONArray jsonarr_s = (JSONArray) jobj.get("list");
        Map<Object, Object> map = new HashMap<Object, Object>(); //error in this line 

        if (jsonarr_s.size() > 0) {

            // Get data for List array
            for (int i = 0; i < jsonarr_s.size(); i++) {
                JSONObject jsonobj_1 = (JSONObject) jsonarr_s.get(i);
                JSONObject jive = (JSONObject) jsonobj_1.get("jive");
                Object names = jsonobj_1.get("displayName");
                Object userid = jive.get("username");
                map.put(names, userid);                  
            }

            return map;
        } else {
            map.put("errorcheck", msg);
        }
        return map;
    }
}

The value empid from the ajax call is not being taken by the service. 该服务未使用ajax调用中的empid值。 And please tell me how to catch the parameters from the url and pass to the rest services. 并请告诉我如何从url捕获参数并将其传递给其余服务。

You will also have to specify the @QueryParam annotation to your getList method: 您还必须在getList方法中指定@QueryParam批注:

@GET
@Produces(MediaType.APPLICATION_JSON)
public Map<Object, Object> getList(@QueryParam("empId") String empid) throws Exception {
    System.out.println("id is"+empid);  //when I try to print the empid,it  displays nothing
    Map<Object, Object> map=reportee.getReportees(empid);  
    return map;             
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM