错误:
此请求标识的资源只能根据请求“accept”头生成具有不可接受特征的响应。
以下是我的ajax代码:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript">
/* function new_element(){ */
$(document).ready(function(){
$("#search").click(function(){
console.log("fetched list");
$.ajax({
url: "http://localhost:8080/SpringMvcJdbcTemplate/listContact",
type : "GET",
dataType : 'json',
/* contentType : "application/json", */
accept : "application/json",
success : function(data) {
alert(this.getResponseHeader("Content-Type"));
console.log("SUCCESS: ", data);
display(data);
},
error : function(e) {
console.log("ERROR: ", e);
display(e);
}
});
});
});
function display(data) {
console.log("inside func list");
var json = "<h4>Ajax Response</h4><pre>"
+ JSON.stringify(data, null, 4) + "</pre>";
$('#feedback').html(json);
}
</script>
控制器类
@JsonView(Views.Public.class)
@RequestMapping(value = "/listContact",
method = RequestMethod.GET,
produces=MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public AjaxResponseBody listContact(ModelAndView model) throws IOException {
List<Contact> listContact = contactDAO.list();
System.out.println("listContact");
List<Contactdup> listContdup = new ArrayList<Contactdup>();
Contactdup contactdup = null ;
AjaxResponseBody result = new AjaxResponseBody();
for(Contact contact:listContact) {
contactdup = new Contactdup();
contactdup.setFname(contact.getFname());
System.out.println("inside for");
System.out.println(contact.getFname());
listContdup.add(contactdup);
}
result.setResult(listContdup);
result.setCode("200");
result.setMsg("");
return result;
}
Ajax响应正文:
package ajaxrespose;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonView;
import net.codejava.spring.model.Contactdup;
import net.codejava.spring.model.Views;
public class AjaxResponseBody {
public List<Contactdup> getResult() {
return result;
}
public void setResult(List<Contactdup> result) {
this.result = result;
}
@JsonView(Views.Public.class)
String msg;
@JsonView(Views.Public.class)
String code;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
@JsonView(Views.Public.class)
List<Contactdup> result;
}
1条答案
按热度按时间hvvq6cgz1#
您调用的路由可能没有返回内容类型“application/json”。