我有一个 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 捕获参数并将其传递给其余服务。
最佳答案
您还必须为 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;
}
关于java - 通过传递输入文本框中的参数,通过 jQuery AJAX 调用 Java EE REST 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54319631/