AJAX响应错误:net :: ERR_EMPTY_RESPONSE [英] AJAX response error: net::ERR_EMPTY_RESPONSE
问题描述
var section = $(this).data('section');
var url ='/ users / delete /'+ id;
if确认(删除帖子)){
$ .ajax({
url:url,
type:'DELETE',
success:function(result){
console.log('Deleting post ...');
window.location.href ='/ users / profile';
},
error:function(err){
console.log(err);
}
});
}
});
});
BACK-END:
router.delete('/ delete /:id',function(req,res,next){
var id = req.params.id;
var section = req.params.section;
var image =;
var author =;
var postRef = firebase.database()。ref (posts /+ section +/+ id);
var userRef = firebase.database()。ref(users / posts /+ id);
var likesRef = firebase.database ).ref(users / likes /+ id);
var hotRef = firebase.database()。ref(hot /+ section +/+ id);
postRef.once('value',function(snapshot){
image = snapshot.image;
author = snapshot.author;
if(firebase.auth()。currentUser .uid.toString()== author){
var file = bucket.file(image);
$ b $ file.delete(function(err,apiResponse){
if( err){
console.log(err);
}
其他{
console.log(成功删除);
postRef.remove();
userRef.remove();
hotRef.remove();
likesRef.remove();
req.flash('success_msg','删除后');
res.send(200);
}
});
}
});
});
状态:
我添加了删除按钮,这样用户就可以删除他的帖子。
当用户点击按钮时,一个AJAX请求是我的Node.js服务器。
但我得到以下错误:
错误:
net :: ERR_EMPTY_RESPONSE
code>
问题: b
什么是这个错误,我该如何解决它?
实际上是正确的。根据文档,Firebase会返回200个状态码, 空的回应。 net :: ERR_EMPTY_RESPONSE
就是这样。你应该做的是在响应中检查 null
和一个 200
状态码。如果确实如此,那么您可以放心地认为该帖子已被删除。
我个人认为Firebase应该考虑返回比 和一个通用的,全面的状态码。我更喜欢204无内容,或410 GONE。但是,唉。
$ b -
注意:如果帖子没有属于作者 - 即使你的条件不匹配,你的API仍然应该返回 (一个错误,可能在这种情况下)。像:
if(firebase.auth()。currentUser.uid.toString()== author){
//你的代码
} else {
res.status(401).send(User does not have permission to complete the operation。)
}
CODE:
FRONT-END
$(document).ready(function(){
$('.delete-post').on('click', function(){
var id = $(this).data('id');
var section = $(this).data('section');
var url = '/users/delete/'+id;
if(confirm("Delete Post ?")){
$.ajax({
url: url,
type:'DELETE',
success: function(result){
console.log('Deleting post...');
window.location.href='/users/profile';
},
error: function(err){
console.log(err);
}
});
}
});
});
BACK-END:
router.delete('/delete/:id', function(req, res, next) {
var id = req.params.id;
var section = req.params.section;
var image = "";
var author = "";
var postRef = firebase.database().ref("posts/"+section+"/"+id);
var userRef = firebase.database().ref("users/posts/"+id);
var likesRef = firebase.database().ref("users/likes/"+id);
var hotRef = firebase.database().ref("hot/"+section+"/"+id);
postRef.once('value', function(snapshot){
image = snapshot.image;
author = snapshot.author;
if (firebase.auth().currentUser.uid.toString() == author) {
var file = bucket.file(image);
file.delete(function (err, apiResponse) {
if (err) {
console.log(err);
}
else {
console.log("Deleted successfully");
postRef.remove();
userRef.remove();
hotRef.remove();
likesRef.remove();
req.flash('success_msg','Post Deleted');
res.send(200);
}
});
}
});
});
SITUATION:
I added delete buttons so the user could delete his posts.
When the user clicks the button an AJAX request is made to my Node.js server.
But I get the following error:
ERROR:
net::ERR_EMPTY_RESPONSE
QUESTION:
What is this error and how do I fix it ?
The response you're getting is actually correct. Per the docs, Firebase returns a 200 status code and an empty response. net::ERR_EMPTY_RESPONSE
is exactly that. What you should do is check for both null
and a 200
status code in the response; if true, you can safely assume that the post was deleted.
My personal opinion is that Firebase should really consider returning something more substantial than nothing and a generic, catch-all status code. I'd prefer something like 204 No Content, or 410 Gone. But, alas.
—
Side note: this conditional will never return anything if the post doesn't belong to the author — your API should still return something (an error, probably in this case) even if your conditional doesn't match. Like:
if (firebase.auth().currentUser.uid.toString() == author) {
// your code
} else {
res.status(401).send("User does not have permission to complete the operation.")
}
这篇关于AJAX响应错误:net :: ERR_EMPTY_RESPONSE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!