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)}
})
运行后出现下面这种错误
弄了一个上午了求大佬告知如何解决!!!