【发布时间】:2015-06-19 15:36:38
【问题描述】:
我正在使用 java 的 restful web 服务
@Path("/data")
public class StudentDataService {
StudentInfo st=new StudentInfo();
@GET
@Path("/mydata")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public List<Student> findAll() {
System.out.println("data returned to the web service");
return st.populateData();
}
}
我正在返回一个包含学生姓名、ID、分数等的数据列表。
尝试使用 jQuery ajax 调用此方法如下:
function findAll() {
console.log('findAll');
$.ajax({
type: "GET",
url: "http://localhost:9297/StudentData/rest/data/mydata",
dataType: "json",
crossDomain: true,
success: function(resp){
// we have the response
alert("Data\n '" + resp + "'");
},
error: function(e){
alert('Error: ' + e);
}
});
}
我遇到错误
它还在控制台上给出错误,例如“未找到元素”
【问题讨论】:
-
对 ajax 使用相对 url。
-
只是猜测:改为 dataType: "json" 尝试 dataType: "application/json"。如果这不起作用,请告诉我们错误消息是什么...
-
您对
error函数参数的假设不正确。它实际上是 xhr 对象。更改功能如下function(jqxhr, status, errorMsg),并提示状态和错误信息,然后反馈给我们。我最初的猜测是你有一个跨域问题 -
将 dataType 更改为 application/json,并将其更改为 function(jqxhr, status, errorMsg) 给出 'Error:parseerror'
-
附带说明:您的代码不是线程安全的。
标签: java jquery ajax rest jax-rs