我想从jsp进行ajax调用:

<script type="text/javascript">

    function isTablePkUsedInFk(table_, pkVal) {

        var url_ = "<c:url value='/ajaxIsTablePkUsedInFk' />";

        var ret = $.ajax({
            data : {"table" : table_ , "pkVal" : pkVal},
            type : "POST",
            url  : url_,
            async: false
        }).responseText;

        ret = $.trim(ret);

        return ret;

    }

</script>


在控制器中:

@Controller
@RequestMapping("/")
public class Home {

    @Autowired
    private Environment env;

    @Autowired
    private UtilisateurDAO utilisateurDao;

    @Autowired
    private MenuDAO menuDao;

    @Autowired
    private DbDAO dbDao;

    @RequestMapping(value = "/ajaxIsTablePkUsedInFk", method = RequestMethod.POST)
    @ResponseBody
    public boolean ajaxIsTablePkUsedInFk(HttpServletRequest request) {
        String table = request.getParameter("table");
        String pkVal = request.getParameter("pkVal");
        return dbDao.isTablePkUsedInFk(table, pkVal);
    }

}


在运行时,当我调用javascript函数isTablePkUsedInFk时没有任何反应!那么如何在javascript中获取Java布尔值呢?

最佳答案

您不能从rest控制器返回原语。控制器可以返回表示此布尔值的String对象。

@ResponseBody
public String ajaxIsTablePkUsedInFk(HttpServletRequest request) {
  ...
  return Boolean.toString(dbDao.isTablePkUsedInFk(table, pkVal));
}

09-27 05:12