Hi i tried to send a PUT request to my slim php api but it seems like that my parameters turns to null. here is my code:
function update() {
$.ajax({
type: 'PUT',
contentType: 'application/json',
url: 'api/edit' + '/' + $('#id').val(),
contentType: "application/json",
data: formToJSON(),
success: function(data, textStatus, jqXHR){
document.location.href="page.php?update=OK"
},
error: function(jqXHR, textStatus, errorThrown){
document.location.href="page.php?update=ERROR"
}
});
}
function formToJSON() {
alert($('#id').val()+' '+$('#param1').val()+' '+$('#param2').val()+' '+$('#param3').val());
return JSON.stringify({
"id": $('#id').val(),
"param1": $('#param1').val(),
"param2": $('#param2').val(),
"param3": $('#param3').val()
}
$('#btnUpdate').click(function() {
update();
return false;
});
Can someone help me out this? Please.
It finally works with this
function update() {
$.ajax({
type: 'PUT',
dataType: 'application/x-www-form-urlencoded',
url: '/api/edit',
data: { id: $('#id').val(),
param1: $('#param1').val(),
param2: $('#param2').val(),
param3: $('#param3').val(),
param4: $('#param4').val()
}
});
}
</div>
I hope this code will work.
function update() {
$.ajax({
type: 'PUT',
contentType: 'application/json',
url: 'api/edit' + '/' + $('#id').val(),
contentType: "application/json",
data: formToJSON(),
success: function(data, textStatus, jqXHR){
document.location.href="page.php?update=OK"
},
error: function(jqXHR, textStatus, errorThrown){
document.location.href="page.php?update=ERROR"
}
});
}
function formToJSON() {
alert($('#id').val()+' '+$('#param1').val()+' '+$('#param2').val()+' '+$('#param3').val());
return JSON.stringify({
"id": $('#id').val(),
"param1": $('#param1').val(),
"param2": $('#param2').val(),
"param3": $('#param3').val()
})
}
$(document).ready(function () {
$('#btnUpdate').click(function() {
update();
return false;
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</div>