Tuesday 16 May 2017

Creating a simple delete button Nodejs

I am writing a nodejs server and I'm a little stuck and hoped that someone could help clarify my code with me.

I have a server that hosts a page and on the click of a button will return a list of names. This is the AJAX function that appends the names of the users and the buttons to the page.

$(function() {
$('#get-button').on('click', function() {
    $.ajax({
        url:'/retrieve',
        contentType: 'application/json',
        success: function(response) {

            //Clear the container first
            $(".container").html("")

            //Parse the response
            var detail = JSON.parse(response.data)
            //Append the response
            for (i = 0; i < detail.people[0].firstname.length; i++) {
                $(".container").append("<div class=\"names\" >"+detail.people[0].firstname[i]+ " " + detail.people[0].surname[i] +"</div> <button id=\"delete\">delete</button>")                   
            }
        }
    })
})
})

Appended to the html page is a delete button. I am happy for this just to console log on the client side in order to get some functionality.

So I have written a function in the server like so

app.get('/delete', function(req,res) {
    console.log("hi")
    res.send("200")
})

Then an AJAX function to pick this up

$(function() {
$('#delete').on('click', function() {
    $.ajax({
        url:'/delete',
        contentType: 'text',
        sucess: function(response) {
            console.log("hi")
        }

    })
})
})

There is no functionality at the moment, I just want the button to work. Am I correct in saying the code works like so

  • html button pressed on html page
  • ajax picks up button press
  • server is pinged
  • ajax waits for server response and continues on response 200

So I want the deletebutton to work and print to the console but it does not.

Just wondered if anyone could clarify where I am going wrong, thanks in advance



via Sprout

No comments:

Post a Comment