I am using this and this to post some data on my rest api. But I get a bad request (400) response.
A piece of my code
var dataTask = {};
var dataPeri = {};
var csrf;
var that = this;
var date = new Date();
var dateString;
dateString = date.toJSON().slice(0, 10);
dataPeri['date'] = dateString;
dataPeri['customer'] = customerId;
csrf = $('input[name="csrfmiddlewaretoken"]').val();
console.log('csrf:'+csrf);
console.log(dataPeri); //The objects seems fine with date at the right format and customerId a number
$.ajaxSetup({
beforeSend: function(xhr, settings){
if (!this.crossDomain){
xhr.setRequestHeader("X-CSRFToken", csrf);
}
}
});
$.ajax({
dataType:'json',
type: 'post',
url: '/crm/api/periodontogramms/',
data:JSON.stringify(dataPeri),
success: function (data, textStatus, jqXHR){
alert("New Periodontogramm saved successfully");
console.log("Periodontogramma "+ data);
that.periodontogramms.push(data);
},
error: function (jqXHR, textStatus, errorThrown){
alert(errorThrown);
}
});
I cannot get what the error is because no other message comes with the error. I am sending Json back.(with JSON.stringify). Should I send something else? Could it be the dateString casuing the issue? On my browsable Api to post something I use date at the following format:YYY-MM-DD. That is what I get from the date.toJSON.slice(0, 10). If it was a csrf issue shouldn't I be getting a forbidden message(403)