$('button.red-button').on('click', function(form)
{
form.preventDefault();
var obj = {};
obj.id = $(this).attr('id');
//var id['id'] = $(this).attr('id');
alert(JSON.stringify(obj));
$.ajax(
{
type: 'POST',
dataType: 'json',
url: 'delete-comment',
data: JSON.stringify(obj),
success: function(data)
{
alert(data);
},
error: function(data)
{
alert(0);
}
});
});
This is what I made.
For example we have this button
<button id="4" class="red-button">Usuń</button>
This script will alert first
{"id":"4"}
So it alerts it's ID in JSON format.
This is what i wanted, to delete this item by doing AJAX POST request.
But in PHP by doing
echo json_encode($this->input->post('id'));
I got NULL in second alert.
or
echo json_encode($_POST['id']);
and i got 0 in second alert
I'm doing something wrong but I don't know... Maybe this json is send wrongly, or json is build wrongly idk
$('button.red-button').on('click', function(form)
{
form.preventDefault();
var obj = {};
obj.id = $(this).attr('id');
//var id['id'] = $(this).attr('id');
alert(JSON.stringify(obj));
$.ajax(
{
type: 'POST',
dataType: 'json',
url: 'delete-comment',
data: obj,
success: function(data)
{
alert(data);
},
error: function(data)
{
alert(0);
}
});
});
Use it like that and it will work, without stringify the object
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.