【问题标题】:Calling Java EE REST service through jQuery AJAX by passing the parameters from input text box通过从输入文本框中传递参数,通过 jQuery AJAX 调用 Java EE REST 服务
【发布时间】:2019-01-23 03:30:27
【问题描述】:

我有一个 html 代码来创建一个接受输入表单用户的输入文本框。并且参数必须与 url 一起传递给休息服务。 这是我的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);
     }
 });
});

这是我的服务:

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

这是我在 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;
    }
}

服务未使用来自 ajax 调用的值 empid。请告诉我如何从 url 中获取参数并传递给其他服务。

【问题讨论】:

    标签: java ajax rest web-services jakarta-ee


    【解决方案1】:

    您还必须为您的 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;             
    }
    

    【讨论】:

      猜你喜欢
      • 2021-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多