I am trying to make a CRUD API and have done the READ part. Rather simple.
Next I am trying to implement the CREATE part. Which works, but randomly sort of. The only pattern I see is that the first insert will fail (new browser), but following inserts work. Theoretically this could be caused by the API, but I suspect that it is the Javascript that is causing this “first-time-error”.
From the form I send this action (the dots .onsubmit. is because the forum does not allow to post correct code):
[code]<form action=”#” .onsubmit.=”api3create(this)” method=”post”>
The Javascript function does two things.
1. Extract the information and create a JSON string.2. Send the call to the API.
The function:
“`
function api3create(elem) {
//get JSON values from form
var form = elem;
var formData = new FormData(form)
var object = {};
formData.forEach(function(value, key) {
object[key] = value;
});
var path = object[‘path’]
var json = JSON.stringify(object)
//call API
var url = “https://api3.go4webdev.org/” + path;
var xhr = new XMLHttpRequest();
xhr.open(“POST”, url);
xhr.setRequestHeader(“Accept”, “application/json”);
xhr.send(json);
}
My question is how to track down the cause of the “first-time-error”. Any clue?
Live: