I am trying to delete a json object from the json file when that object values are displayed in a form using the AJAX Delete function. I am using express to generate a route for the json file like below:
app.get('/users', (req, res) => {
var userData = json;
res.send(userData);
}),
app.delete('/users/delete/:id', (req, res) => {
var key = req.params.id;
var deletedData = json;
deletedData.splice(key, 1);
res.send(deletedData);
});
$(document).on('click', '#delete', function () {
$.ajax({
type: "DELETE"
, url: "users/delete/"+data[key]
, datatype: "datatype"
, success: function (data) {
console.log(data);
}
});
});
I know I havent declared the data[key], that is just context for what i would like to do. My issue is trying to select the specific json object rather than just selecting the first object with what I have now "users/delete/0', would anyone know how to do this?
EDIT
I am trying to get the "users/delete/:id" specific to the object that is displayed in my form in the html
<div class="col-md-5 col-sm-12">
<div id="full_details">
<ul class="details">
<h2>Contact Details</h2> <img src="" alt="Avatar" id="avatar">
<li>Name
<input type="text" id="name" class="form-control mr-sm-2" disabled>
</li>
<li>Address
<input type="text" id="address" class="form-control mr-sm-2" disabled>
</li>
<li>Contact Number
<input type="text" id="phone" class="form-control mr-sm-2" disabled>
</li>
<li>Email
<input type="text" id="email" class="form-control mr-sm-2" disabled>
</li>
<li>Postcode
<input type="text" id="postcode" class="form-control mr-sm-2" disabled>
</li>
<li>City
<input type="text" id="city" class="form-control mr-sm-2"disabled>
</li>
<li>Country
<input type="text" id="country" class="form-control mr-sm-2" disabled>
</li>
<div class="row">
<div class="col-md-6">
<div id="editbutton">
<p class="btn btn-primary">Edit Details</p>
</div>
</div>
<div class="col-md-6">
<div id="savebutton">
<p class="btn btn-danger">Save Details</p>
</div>
</div>
<div class="col-md-6">
<div id="delete">
<p class="btn btn-danger">delete</p>
</div>
</div>
</div>
</ul>
</div>
</div>