javascript - 播放框架删除请求
问题描述
希望有人可以帮助我,我正在尝试获取删除请求以在游戏框架中工作。在我的 html 页面中,我有一个按钮,该按钮正在使用 ajax 语句,但目前我收到一个无法读取属性“addeventlistener”的空错误。我对这一切都很陌生,并且正在学习一个教程,但这使用了我现在试图通过的 onclick 方法。谢谢你
document.getElementById("myBtn").addEventListener("click" ,sendDeleteRequest);
function sendDeleteRequest(url, rUrl) {
$.ajax({
url: url,
method: "DELETE",
success: function () {
window.location = rUrl;
},
error: function() {
window.location.reload();
}
});
}
和我的html
<button class ="btn btn-danger" id="myBtn('@routes.BooksController.destroy(book.id)',
'@routes.HomeController.index()'
)" >Delete</button>
解决方案
你混淆了你的id
属性和其他属性。
使用data-
属性来存储您的路线:
<button class ="btn btn-danger"
id="myBtn"
data-url="@routes.BooksController.destroy(book.id)"
data-redirect-url="@routes.HomeController.index()"
>Delete</button>
that.getAttribute()
并在 Javascript 中使用它们:
document.getElementById("myBtn").addEventListener("click" ,sendDeleteRequest);
function sendDeleteRequest() {
let that = this;
$.ajax({
url: that.getAttribute('data-url'), //Here
method: "DELETE",
success: function () {
window.location = that.getAttribute('data-redirect-url'); //Here
},
error: function() {
window.location.reload();
}
});
}
推荐阅读
- python - 如果字母数字在单词/句子中,如何大写
- sql - Yii2 sql 查询可以从模型属性/函数(不在数据库中)中选择具有值的自定义列吗?
- python - Python拆分和计算
- google-cloud-platform - 如何从计算引擎使用报告中获取任何实例的 cpu 使用情况?
- auth0 - 使用 lightspeed、zapier、auth0 自动登录/多通道登录
- c++ - 这面旗帜的目的是什么?
- c++ - 调试以发布模式构建的库
- r - 从R中的pdf框中提取数据
- mysql - 存储过程mysql输入参数视为null
- angular - NgRx 选择器在效果单元测试中不起作用