I am having some trouble with utilising AJAX to update a field within a record.
It seems that it mostly works but then cannot complete the action of updating the field with the sample data.
I get the console logs:
PUT https://api.knack.com/v1/pages/scene_XXX/views/view_YYY/records/YYY 400 (Bad Request)
Uncaught Error.
{“errors”:[{“message”:“Invalid request”}]}
function updateFieldWithHello(recordId) {
$.ajax({
url: "https://api.knack.com/v1/pages/scene_XXX/views/view_YYY/records/" + recordId, // Scene/View of the form
type: "PUT",
headers: {
"X-Knack-Application-Id": Knack.application_id,
"X-Knack-REST-API-Key": "knack",
"Authorization": Knack.getUserToken(),
"Content-Type": "application/json"
},
data: JSON.stringify({
field_XXX: "hello"
}),
success: function(response) {
console.log('Record Updated');
},
error: function(jqXHR, exception) {
var msg = '';
if (jqXHR.status === 0) {
msg = 'Not connected.\n Verify Network.';
} else if (jqXHR.status == 404) {
msg = 'Requested page not found. [404]';
} else if (jqXHR.status == 500) {
msg = 'Internal Server Error [500].';
} else if (exception === 'parsererror') {
msg = 'Requested JSON parse failed.';
} else if (exception === 'timeout') {
msg = 'Time out error.';
} else if (exception === 'abort') {
msg = 'Ajax request aborted.';
} else {
msg = 'Uncaught Error.\n' + jqXHR.responseText;
}
console.log(msg);
}
});
}
// Listen for form submission to update the record with "hello"
$(document).on('knack-form-submit.view_XXX', function(event, view, record) {
const recordId = record.id;
updateFieldWithHello(recordId);
});