/    Sign up×
Community /Pin to ProfileBookmark

CREATE not working the first time

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”>[/code]

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: https://crud.go4webdev.org/api3create (work in progress…)

    to post a comment
    JavaScript

    2 Comments(s)

    Copy linkTweet thisAlerts:
    @SempervivumJan 11.2022 — Be aware that any submit will cause a reload of the page and I'm not shure if the ajax request will be transmitted reliably when this happens. You should prevent this default action of the submit:
    ``<i>
    </i>&lt;form action="#" .onsubmit.="api3create(this, event)" method="post"&gt;<i>
    </i>
    `</CODE>
    <CODE>
    `<i>
    </i>function api3create(elem, event) {
    event.preventDefault();
    //get JSON values from form
    var form = elem;
    var formData = new FormData(form)
    // remaining code unchanged<i>
    </i>
    ``

    (not tested)

    (another way to prevent the default action is returning false, however in the past I experienced sometimes that this didn't work. Therefore I prefer preventDefault)
    Copy linkTweet thisAlerts:
    @sibertauthorJan 12.2022 — > @Sempervivum#1641392 You should prevent this default action of the submi

    Amazing but not Surprising! It worked perfectly. Thank you!
    ×

    Success!

    Help @sibert spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 4.20,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...