java—我试图使用ajax获取数据库内容并显示,但我看到了这个错误

im9ewurl  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(362)

错误:
此请求标识的资源只能根据请求“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>&lt;pre&gt;"

            + JSON.stringify(data, null, 4) + "&lt;/pre&gt;";
    $('#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;
}
hvvq6cgz

hvvq6cgz1#

您调用的路由可能没有返回内容类型“application/json”。

相关问题