0

I have this code on server side

 router.delete('/categories/delete/:id', function(req,res){
         var id = req.params.id;
          Category.remove({_id: id},function(err){
               if(err){
                    console.log(err);
               }
               req.flash('success','Category Deleted');
               res.location('/manage/categories');
               res.redirect('/manage/categories');
          });
    });

In dustjs view I have this button <a href="#" class="removeCategory button tiny alert" data-id="{._id}">Delete</a>

And in the script file i have

$(document).ready(function(){
    $('.removeCategory').on('click', function(e){
        $target =  $(e.target);
        var id = $target.attr('data-id');
        $.ajax({
            type: 'DELETE',
            url: '/manage/categories/delete/'+id,
            success: function(response){
                alert('Delete');
                window.location = '/manage/categories';
            },
            error: function(err){
                console.log(err);
            }

        });
    });

});

The thing is that it kinda works but the category disappears only after I refresh the page but when i have look at the console I get a message like this:

abort: function abort()
​
always: function always()
​
complete: function add()
​
done: function add()
​
error: function add()
​
fail: function add()
​
getAllResponseHeaders: function getAllResponseHeaders()
​
getResponseHeader: function getResponseHeader()
​
overrideMimeType: function overrideMimeType()
​
pipe: function then()
​
progress: function add()
​
promise: function promise()
​
readyState: 4
​
responseText: "<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n<meta charset=\"utf-8\">\n<title>Error</title>\n</head>\n<body>\n<pre>Cannot DELETE /manage/categories</pre>\n</body>\n</html>\n"
​
setRequestHeader: function setRequestHeader()
​
state: function state()
​
status: 404
​
statusCode: function statusCode()
​
statusText: "Not Found"
​
success: function add()
​
then: function then()

I also want to mention that the alert box never appears. Does anyone know how to fix this issue? Thank you in advance

3
  • What is the server side code? Which framework ? Commented Apr 16, 2018 at 12:51
  • It seems like you're not actually returning the response to the client from your server side, and that your success callback on the client isn't being triggered because of that. Commented Apr 16, 2018 at 12:52
  • Thank you for you opinion. I will have a look on that Commented Apr 16, 2018 at 16:57

1 Answer 1

1

seems like the url you are calling is not correct, the responseText property gives a hint on that:

Cannot DELETE /manage/categories

thats why your error Handler is called:

error: function(err){
  console.log(err);
}

and not the success.

Are you sure the id is determined correct?

Sign up to request clarification or add additional context in comments.

8 Comments

Yes the id is correct and the link i access all the categories is: localhost:3000/manage/categories That s why the url is /manage/categories/delete/'+id to match the server side code and then i want to reload the page I m using /manage/categories in window.location If I don t refresh the page nothing happens, once i refresh after i clicked the button i get the flash message and the category goes away
I fixed it by removing the # from a link. I left it empty. You were right the link it was the problem apparently. Thank you for your answer
However I still get that message in console and the alert message never triggers, but I get the flash message and the category is removed
does this route supports the method 'delete' ? can you send the part of your server side code where you process the response?
I added a complete method right after error method in front end script and in that method i added the window.location and the console and now I get the message in the console.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.