Miyako,
Thanks very much for pointing me in the right direction.

Let me share how I've got this working. It's better but still feels a
little kludgey.

On the javascript side here's how I changed the post method:

var postData = JSON.stringify(woData);

$.ajax({
url: '/workorder',
type: 'POST',
contentType: 'plain/text; charset=utf-8',
processData: false,
data: postData
})
.done(function(data) {
console.log(data);
// check data for error message
if(data.error){  // show the error
swal(data.error, '', 'warning');
} else {
swal(data.pageMsg, '', 'success');
}
})
.fail(function() {
swal('Fail!', '', 'warning');
})
.always(function() {
//   alert( "complete" );
});
}

'contentType' refers to the data being SENT to the server. 'dataType',
which I didn't define, deals with the data returned.

I could not get 4D to handle the JSON contentType directly so I stringify
it and send it as plain text. I'm probably missing something about how to
manage that.

'processData' is false to prevent converting it into a query string.
Converting it might not be a bad thing if you are sending form data
directly. I'm not so it didn't help.

On the 4D side all I need is:

WEB GET HTTP BODY($body)

and $body contains the text of postData which I can easily parse into a
c-object.

I played with WEB GET HTTP BODY PARTS and here is where the processData
option from above comes into play. If it's omitted or TRUE data are
converted to a query string and each 'element' is a body part. In my case
it meant instead of one element (the stringified JSON) there were about 50
or so (each key/value of the JSON). That might be desirable if I had an
array of checkboxes, for instance.

With processData = false the data is submitted as a single block and that's
why WEB GET HTTP BODY returns the complete JSON string. Interestingly if I
process it by body parts (there's only 1) it's broken into the same two
parts I found in the first place in the name/value pair.

There are a ton of useful examples to be found. This one is pretty complete
and shows how to start/stop a progress indicator (loader):

https://webdesign.tutsplus.com/tutorials/how-to-use-jquerys-ajax-function--cms-25774

this one is non-intimidating and has a concise list of $.ajax options:

https://www.sitepoint.com/use-jquerys-ajax-function/



On Tue, Apr 4, 2017 at 5:35 PM, Keisuke Miyako via 4D_Tech <
4d_tech@lists.4d.com> wrote:

> WEB GET VARIABLES only parses form variables (x-www-form-urlencoded).
>
> and then, jQuery post() only sends x-www-form-urlencoded.
>
> http://stackoverflow.com/questions/5529685/post-doesnt-
> send-data-as-json-but-as-x-www-form-urlencoded-instead
>
> so with this setup,
> you are basically working with a string variable whose name is "data"
> and whose content just happens to look like JSON.
>
> if you want to directly work with JSON,
> then you need to use jQuery ajax (not post) as explained in the SO post
> above,
> and use
> WEB GET HTTP HEADER
> WEB GET HTTP BODY PARTS/WEB GET HTTP BODY
> JSON Parse/JSON PARSE ARRAY
> with appropriate error handling instead of WEB GET VARIABLES.
>
>
>
> **********************************************************************
> 4D Internet Users Group (4D iNUG)
> FAQ:  http://lists.4d.com/faqnug.html
> Archive:  http://lists.4d.com/archives.html
> Options: http://lists.4d.com/mailman/options/4d_tech
> Unsub:  mailto:4d_tech-unsubscr...@lists.4d.com
> **********************************************************************
>



-- 
Kirk Brooks
San Francisco, CA
=======================

*The only thing necessary for the triumph of evil is for good men to do
nothing.*

*- Edmund Burke*
**********************************************************************
4D Internet Users Group (4D iNUG)
FAQ:  http://lists.4d.com/faqnug.html
Archive:  http://lists.4d.com/archives.html
Options: http://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:4d_tech-unsubscr...@lists.4d.com
**********************************************************************

Reply via email to