sir*_*tty 5 ajax jquery node.js
我正在编写node.js应用程序,我有点担心如何构建发送数据以发送到服务器.例如,当我想删除数据项时,我将所述项的id放在href属性中:
<a class='delete' href="1231">Delete this data</a>
<!-- href is based on a variable I'm pulling from the server -->
Run Code Online (Sandbox Code Playgroud)
单击该链接时,我会阻止默认操作,然后运行ajax请求:
//On click + ajax
body.on('click', '.delete', function(e){
e.preventDefault();
$.ajax({
type: 'POST',
url: '/admin/report/detail/delete',
data: {id: $(this).attr('href')},
success: function(){
//Success message
},
error: function(){
//Error message
}
});
});
Run Code Online (Sandbox Code Playgroud)
我想知道,以这种方式使用href属性是不好的做法吗?如果是这样,存储这些数据的最佳方法是什么?
请改用数据属性.将它们存储在href中并不是语义的,如果您希望有时存储ID,以及其他时间存储其他数据,该怎么办?您可以根据需要创建任意数量的数据属性,为其提供语义名称.
<a class="delete" data-id="1231" href="#">
Run Code Online (Sandbox Code Playgroud)
然后在你的JavaScript中:
...
data: { id: $(this).data('id') }
...
Run Code Online (Sandbox Code Playgroud)
要么
data: { id: $(this).attr('data-id') }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
637 次 |
| 最近记录: |