后台使用jfinal3.3,前台页面jsp+jquery3.2.1,项目部署在tomcat8.0下,使用localhost访问一切正常,但是使用127.0.0.1或者本机域名打开项目,查询数据库错误。
前台页面代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="http://localhost/offer/js/jquery-3.2.1.min.js"></script>
</head>
<body>
area.index.jsp
<button id="tb" onclick="test()">test</button>
<script type="text/javascript">
function test() {
var s = '';
$.ajax({
type : "POST",
url : "http://localhost/offer/area/test",
data : {
"level" : 1
},//用json对象传参给后台
dataType : 'json',//返回值是json
async : false,//同步请求
success : function(data) {
//alert(data);
//var ls = data.results;
$.each(data, function(i, area) {
var n = i + 1;
if (n < 10) {
n = "0" + n;
}
s += ('<p>编号:' + n + ' | ' + 'id:' + area.id
+ ' | name:' + area.name + '</p><hr/>');
});
$('#out').html(s);
},
error : function(data) {
alert("请求失败!请联系管理员!");
}
});
}
</script>
<div id="out"></div>
</body>
</html>
后台java代码:
public class AreaOfferController extends Controller {
AreaOfferService aos = new AreaOfferService();
public void test() {
String level = getPara("level");
if (null == level || "".equals(level) || Integer.parseInt(level) == 0) {
setAttr("result", "0");
renderJson();
} else {
renderJson(aos.getAreaOffersByLevel(Integer.parseInt(level)));
}
}