This turned out, after some pointers in another question, to be an access issue. However advice here on json.loads() and request.body() also formed part of the answer. Another additional was JSON.stringify() and that instead of nesting an array in an array, I nested an object inside an array - not sure how much difference that made but its in the end result.
The JSON being sent was valid. After adding the json.loads() and the using request.body instead of request.POST, I still needed to access the values and keys which I will share below:
JQuery:
$(document).on('click','#exModel',function () {
const sending = [];
$("table tr").each(function () {
var p1 = $(this).find("th label").html();
var p2 = $(this).find("td input").attr('id');
var p3 = $(this).find("td input").val();
const build = {bnaa:p1,id:parseInt(p2),vals:parseInt(p3)};
sending.push(build);
// build.push(p1, p2, p3);
});
console.log(sending);
//console.log(JSON.parse(JSON.stringify(sending)));
$.ajax({
url: '../coreqc/exModel/',
data: JSON.stringify({'sending':sending}),
//data: {'sending':sending},
//processData: false,
type: 'POST',
headers: {'content_type':'application/json','X-CSRFToken': '{{ csrf_token }}'},
// headers: {'X-CSRFToken': '{{ csrf_token }}'},
async: 'true',
success: function (data) {
console.log("I made it back")
//dom: 'Bfrtip',
}
});
Then in my view:
def exModel(request):
data = request.body
data = json.loads(request.body)
print(type(data))
for i in range(len(data['sending'])):
print(data['sending'][i]['bnaa'])
print(data['sending'][i]['id'])
print(data['sending'][i]['vals'])
template = loader.get_template('coreqc/tester.html')
context = {
#'blabla':blabla
}
return HttpResponse(template.render(context, request))
Note I have not dealt with the response yet but the above will give you access to individual values, which will print to your terminal.