javascript - 发生违反约束时显示警报消息
问题描述
晚上好,
我向您展示我的界面,以便您更好地理解:
我在 2 列(local_name 和 number_place)上添加了一个唯一约束,一切正常,只是有一个小问题,我想添加一个带有已经存在的地号的本地名称,它让我回到这样的空白页面:
它向我显示了 NetBeans 上的错误,即我违反了约束……而我希望它不要将我重定向到空白页面,但它会停留在同一页面上,并且它会向我显示一条类型的消息:
错误您输入了两个具有相同地点编号和相同本地名称的场所
我尝试了一些 javascript 代码和 ajax 但它在这里不起作用是我在我的 servlet jee 中添加的代码:
if("ajouter".equals(action)){
PreparedStatement st1=c.prepareStatement("insert into place values (null,?,?,?,?)");
st1.setString(1, nom_local);
st1.setInt(2, Integer.parseInt(numeroplace));
st1.setString(3, prixplace);
st1.setString(4, tailleplace);
int resadd=st1.executeUpdate();
if (resadd>0){
request.setAttribute("msg", "place ajouté avec succés !");
}
else {
request.setAttribute("msg", "place non ajouté !");
throw new Exception("Erreur vous avez entrez deux locaux ayant le meme numéro de place et le meme nom de local" );
}
getServletContext().getRequestDispatcher("/WEB-INF/Place_locaux.jsp").forward(request, response);
}
这是javascript代码:
<script>
jQuery.ajax({
type: POST,
url: Place_locaux.java,
success: function (response) {
//Code en cas de succès de l'appel
},
error: function (xhr, ajaxOptions, thrownError) {
alert(chr.responseText); //Ce code affichera le message d'erreur.
}
});
</script>
我希望我已经清楚了,我期待您的回复,我要提前感谢您的回复
解决方案
推荐阅读
- javascript - 扩展成功后 AWS AutoScaling Down 策略失败:无法执行自动扩展操作:未找到指标值的步进调整
- javascript - 请为我解释这个java脚本代码
- git - Git 将一个分支的内容复制到另一个分支
- reactjs - React Native:将多个联系人保存到电话联系人
- azure - Azure Blob 存储生命周期管理 - 在容器级别设置筛选器
- django - 鹡鸰管理员过滤器按钮
- c# - 如何使用 Microsoft.Office.Interop 自动从 Excel 中选择数据
- javascript - 贝宝订阅集成 | 与捕获资金混淆
- objective-c - Objective c / 如何将用户实现的静态函数调用到 nsstring 类中?
- python - Python shell csv 问题 FileNotFoundError: [Errno 2] No such file or directory: 'iris.csv'