jfinal结合ajax

前台代码

$.ajax({
    type: "post",
    dataType: 'json',
    url: '/checkCity',
    data: {cityname: $('#autocomplete').val()},
    success:function(res){
        if(res.err == 0) {
            window.location.href = '/'+res.cityname;
        } else {
            alert('对不起,该城市不存在!')
        }
    }
});

后台代码

//加入Controller
add("/checkCity", CheckCityController.class);

//Controller写方法,返回参数
public class CheckCityController extends Controller {

    public void index() {
        String cityname = getPara("cityname");
        ArrayList cities = City.getAllCities();
        if (cities.contains(cityname)) {
            setAttr("err", 0);
            setAttr("cityname", cityname);
        } else {
            setAttr("err", 1);
        }
        renderJson();
    }

}

可以参考示例
https://github.com/lincolnmi/AirQualitiesShow

你可能感兴趣的:(web工程)