我不明白如何使用jquery从我的前端发送请求到后端,以触发应搜索数据库并删除/更新文档的功能。我只想按一个按钮删除/更新文档。一切正常如果我通过Postman提交请求。我是节点的初学者,我无法找到解决方案。
app.js
app.delete('/delete/:id', (req, res) => {
let id = req.params.id;
if (!ObjectID.isValid(id)) {
return res.status(400).send();
}
Blog.findByIdAndRemove(id).then((docs) => {
res.status(200).send({docs})
}).catch((e) => {
res.status(400)
});
});
app.patch('/update/:id', (req, res) => {
var id = req.params.id
var update = {autore: req.body.autore}
if (!ObjectID.isValid(id)) {
res.status(400).send()
} Blog.findByIdAndUpdate(id, update, {new: true}).then((docs) => {
res.send({docs});
}).catch((e) => {
res.status(400)
});
});

HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Titolo</title>
</head>
<body>
<button id:"cancella" type="submit">Submit</button
<script src="/js/jquery.js"></script>
<script src="/js/script.js"></script>
</body>
</html>
&#13;
这个script.js文件是否也必须由后端管理?
提前谢谢!
的script.js
$(document).ready(function() {
$('#cancella').on("click", function() {
$.ajax({
url: '/delete/:id',
type: 'DELETE',
data: {"_id": 5984f1a8d2c11b0354d1601b},
dataType: 'json',
success: function(data) {
console.log('Dati cancellati!' + data);
}});
});
});
&#13;
答案 0 :(得分:0)
在您jQuery
id
body
node
query parameters
jQuery
body
$(document).ready(function() {
$('#cancella').on("click", function() {
var baseURL = 'http://localhost' // depending of your config you should set this
$.ajax({
url: baseURL + '/delete/' + 5984f1a8d2c11b0354d1601b,
type: 'DELETE',
success: function(data) {
console.log('Dati cancellati!' + data);
}});
});
});
中let todoList = {
// create an empty array
todos: [],
// display all todos
displayTodos: () => {
// check if array is empty
if (this.todos.length === 0) {
console.log('Todos is empty');
} else {
// loop through array
for (var i = 0; i < this.todos.length; i++) {
// check completion status
// prefix x if completed
if (this.todos[i].completed === true) {
console.log('(x)', this.todos[i].todoText);
} else {
console.log('( )', this.todos[i].todoText);
}
}
}
},
// add todo to todo Objects
addTodos: (todoText) => {
this.todos.push({
todoText: todoText,
completed: false
});
this.displayTodos();
},
// change todo at certain position
changeTodos: (position, todoText) => {
this.todos[position].todoText = todoText;
this.displayTodos();
},
// change the completion status of todo
toggleCompleted: (position) => {
this.todos[position].completed ? false : true;
this.displayTodos();
}
};
todoList.addTodos('Test todo 1');
todoList.addTodos('Test todo 2');
todoList.addTodos('Test todo 3');
todoList.changeTodos(1, 'Test todo 4');
todoList.toggleCompleted(1);
您应该更改@Path("/somePath")
@RequestScoped
public class SomeResource {
@Inject
private TestBean testBean;
@GET
public String doGet() {
return testBean.getThis();
}
}
网址,而无需发送任何public static <T> Set<Class<? extends T>> dosomething(Class<T> clazz)
:
Class<?> clazz = Integer.class
Set<Class<?>> result = dosomething(clazz);
这应该可以解决问题