Javascript:
app.post('/article/add', function(req, res){
let article = new Article();
article.title = req.body.title;
article.author = req.body.author;
article.body = req.body.body;
article.save(function(err){
if(err){
res.end("Error by save! " + err);
}else{
console.log("Saved");
res.send({status: "ok", message: "Received."});
}
});
});
#########################################################ajax
$(document).ready(function(){
$('.post-article').on('click', function(e){
var data = $('.autor').val();
console.log("Data: " + data);
$.ajax({
type: 'post',
url: 'http://localhost:3000/article/add',
success: function(response){
alert('Ein Person wurde mit dem Namen: ' + data + ' hinzugefügt');
window.location.href='/';
},
error: function(err){
console.log('Ajax, Post Error! ' + err);
}
})
});
)};
Ich versuche, nach dem Article in der Datenbank gespeichert wird, Alert ausgeben zu lassen.
Alert wird angezeigt, aber dann kommt diese schreckliche Fehlermeldung:
Waiting for the debugger to disconnect...
events.js:292
throw er; // Unhandled 'error' event
^
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:558:11)
at ServerResponse.header (C:\Beka\OMP\Visual Studio Code\projectJS\node_modules\express\lib\response.js:771:10)
at ServerResponse.send (C:\Beka\OMP\Visual Studio Code\projectJS\node_modules\express\lib\response.js:170:12)
at ServerResponse.json (C:\Beka\OMP\Visual Studio Code\projectJS\node_modules\express\lib\response.js:267:15)
at ServerResponse.send (C:\Beka\OMP\Visual Studio Code\projectJS\node_modules\express\lib\response.js:158:21)
at C:\Beka\OMP\Visual Studio Code\projectJS\app.js:77:13
at C:\Beka\OMP\Visual Studio Code\projectJS\node_modules\mongoose\lib\model.js:5074:18
at processTicksAndRejections (internal/process/task_queues.js:75:11)
Emitted 'error' event on Function instance at:
at C:\Beka\OMP\Visual Studio Code\projectJS\node_modules\mongoose\lib\model.js:5076:15
at processTicksAndRejections (internal/process/task_queues.js:75:11) {
code: 'ERR_HTTP_HEADERS_SENT'
}
und Programm ist selbstverständlich aus.
Vielen Dank im Voraus