Web Connection
Fetch with JSON.Stringify not Posting Form Data
Gravatar is a globally recognized avatar based on your email address. Fetch with JSON.Stringify not Posting Form Data
  Marty
  All
  Jun 5, 2020 @ 09:24am

I'm trying to get away from jQuery by using fetch to post form data.

I have no trouble using formData = new FormData(frmMain) to post data using fetch - However the example below is from the developer.mozilla.org web site and it doesn't work for me. The web server does not read in the stringified JSON.

postData(`http://example.com/answer`, {answer: 42})
  .then(data => console.log(JSON.stringify(data)))
  .catch(error => console.error(error));

function postData(url = ``, data = {}) {
    return fetch(url, {
        method: "POST",
        mode: "cors",
        cache: "no-cache", 
        credentials: "same-origin",
        headers: {
            "Content-Type": "application/json"
        },
        redirect: "follow",
        referrer: "no-referrer",
        body: JSON.stringify(data)
    })
    .then(response => response.json()); // parses response to JSON
}


Gravatar is a globally recognized avatar based on your email address. re: Fetch with JSON.Stringify not Posting Form Data
  Rick Strahl
  Marty
  Jun 5, 2020 @ 10:53am

Are you making a cross site call? Differnent Web site or even same machine, different port? If so, CORS may be preventing the calls from working...

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: Fetch with JSON.Stringify not Posting Form Data
  Marty
  Rick Strahl
  Jun 5, 2020 @ 11:21am

The web server registers the requested data OK, but variables do not get imported . I'm using

Dimension laVars[1,2] lnVars = Request.aFormVars(@laVars)

It that CORS related? I'm using localhost/wconnect as the server.

Gravatar is a globally recognized avatar based on your email address. re: Fetch with JSON.Stringify not Posting Form Data
  Rick Strahl
  Marty
  Jun 5, 2020 @ 11:40am

That's because JSON is not a form variable request. You can only get the raw JSON data. Use Request.Form() to retrieve the JSON then deserialize it and get the value out. Or use a REST Service which will handle all that for you.

+++ Rick ---

© 1996-2024