[英]Calling Java EE REST service through jQuery AJAX by passing the parameters from input text box
我有一個html代碼來創建一個接受輸入表單用戶的輸入文本框。參數必須與url一起傳遞給rest服務。 這是我的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捕獲參數並將其傳遞給其余服務。
您還必須在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.