ssm后台返回一个list对象,一直报错

package controller;

//这是后台请求代码
import com.fasterxml.jackson.annotation.JsonInclude;
import org.springframework.aop.target.LazyInitTargetSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import pojo.Food;
import service.FoodService;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.util.List;
@JsonInclude(JsonInclude.Include.NON_NULL)
@RestController
public class JspController {
@Autowired
@Qualifier("FoodServiceImpl")
private FoodService foodService;

@RequestMapping(value = "/title", produces = "text/html;charset=UTF-8;")
public List<Food> test2()  {
    List<Food> list=foodService.getFoodList();
  System.out.println(list);
    return  list;
}

}

//这是前台页面ajax代码
 $.ajax({
        url:"${pageContext.request.contextPath}/title",
        //解决编码问题
        contentType: "application/x-www-form-urlencoded; charset=utf-8",
        success:function ( data) {
          var html="";
          if(data==null)return;
          else {
              for (let i = 0; i < data.length; ++i) {
                  html += "<div class=\"card \" style=\"width: 18rem;float:left\">" +
                      "  <img src=\"picture/b.jpg\" class=\"card-img-top\" alt=\"...\">" +
                      "  <div class=\"card-body\">" +
                      "    <h5 class=\"card-title\">xxx</h5>" +
                      "    <p class=\"card-text\">xxxx</p>" +
                      "    <a href=\"/context1\" class=\"btn btn-primary\">详情</a>" +
                      "  </div>" +
                      "</div>";
              }

              $("#context").html(html);
          }
        },error:function(xhr){alert(xhr.responseText)}
      })

运行后出现下面这种错误
图片说明
弄了一个上午了求大佬告知如何解决!!!

2个回答

("Content-Type", "application/json;charset=UTF-8")
可以去参考一下这里:
https://blog.csdn.net/lhq186/article/details/96035697

qq_40299125
qq_40299125 还是不可以啊,可以运行了,但前端接收到的数据不是列表,变成了[Object object]/哭/哭
大约 2 个月之前 回复

在你的后台方法上加入 @ResponseBody 注解试下

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问