Question about passing JSON to 4D

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Question about passing JSON to 4D

4D Tech mailing list
Hi folks,

I need a little guidance here. I have a web form (in a browser) that is
submitting data to 4D. I collect the data on the webform into an object
then stringify it for passing to 4D - like so:

var postData = JSON.stringify(woData);
$.post( "/workorder/data", postData)
.done(function(data) {
if(data.error){ // show the error
swal(data.error, '', 'warning');
} else {
swal(data.pageMsg, '', 'success');
}
})
.fail(function() {
swal('Fail!');
})
.always(function() {
// alert( "complete" );
});


​I​ expected (because I saw it work this way in Tim Penner's great example
db) on the 4D side I could do something like this:

ARRAY TEXT($at_names;0)
ARRAY TEXT($at_values;0)
WEB GET VARIABLES($at_names;$at_values)

​and $at_names{1} would equal "data" and ​$
at_values
​{1} would be the JSON.

Instead what I get is ​

$at_names{1} = first part of the JSON
$at_values{1} = the second part of the JSON

​Now, the good news is this seems to be consistent. I can concatenate the
two parts and parse it fine.
But I don't understand why it's working this way.

​The data payload varies in length but isn't huge. It contains a few
fields, some text and a couple of JSON arrays. It is too long to encode
into the URL though so

$.post( "/workorder?data=" + postData)

​doesn't work. (I tried.)

​I'm hoping someone can either correct my approach or assure me it's OK.

Thanks, I really appreciate the help. ​


--
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:[hidden email]
**********************************************************************
Reply | Threaded
Open this post in threaded view
|

Re: Question about passing JSON to 4D

4D Tech mailing list
WEB GET HTTP BODY($body)
http://doc.4d.com/4Dv15/4D/15.4/WEB-GET-HTTP-BODY.301-3275031.en.html

> On Apr 4, 2017, at 5:16 PM, Kirk Brooks via 4D_Tech <[hidden email]> wrote:
>
> Hi folks,
>
> I need a little guidance here. I have a web form (in a browser) that is
> submitting data to 4D. I collect the data on the webform into an object
> then stringify it for passing to 4D - like so:
>
> var postData = JSON.stringify(woData);
> $.post( "/workorder/data", postData)
> .done(function(data) {
> if(data.error){ // show the error
> swal(data.error, '', 'warning');
> } else {
> swal(data.pageMsg, '', 'success');
> }
> })
> .fail(function() {
> swal('Fail!');
> })
> .always(function() {
> // alert( "complete" );
> });
>
>
> ​I​ expected (because I saw it work this way in Tim Penner's great example
> db) on the 4D side I could do something like this:
>
> ARRAY TEXT($at_names;0)
> ARRAY TEXT($at_values;0)
> WEB GET VARIABLES($at_names;$at_values)
>
> ​and $at_names{1} would equal "data" and ​$
> at_values
> ​{1} would be the JSON.
>
> Instead what I get is ​
>
> $at_names{1} = first part of the JSON
> $at_values{1} = the second part of the JSON
>
> ​Now, the good news is this seems to be consistent. I can concatenate the
> two parts and parse it fine.
> But I don't understand why it's working this way.
>
> ​The data payload varies in length but isn't huge. It contains a few
> fields, some text and a couple of JSON arrays. It is too long to encode
> into the URL though so
>
> $.post( "/workorder?data=" + postData)
>
> ​doesn't work. (I tried.)
>
> ​I'm hoping someone can either correct my approach or assure me it's OK.
>
> Thanks, I really appreciate the help. ​
>
>
> --
> 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:[hidden email]
> **********************************************************************

**********************************************************************
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:[hidden email]
**********************************************************************
Reply | Threaded
Open this post in threaded view
|

Re: Question about passing JSON to 4D

4D Tech mailing list
In reply to this post by 4D Tech mailing list
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:[hidden email]
**********************************************************************
Reply | Threaded
Open this post in threaded view
|

Re: Question about passing JSON to 4D

4D Tech mailing list
On Wed, Apr 5, 2017 at 10:35 AM, Keisuke Miyako via 4D_Tech <
[hidden email]> wrote:

> WEB GET VARIABLES only parses form variables (x-www-form-urlencoded).
>
> and then, jQuery post() only sends x-www-form-urlencoded.

...snip...

Thanks for the fantastic answer. I can easily imagine spending a couple of
(very frustrating) hours trying to figure that out.

Is there any chance that you could get this added to the docs and/or made
into a Tech Tip?
**********************************************************************
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:[hidden email]
**********************************************************************
Reply | Threaded
Open this post in threaded view
|

Re: Question about passing JSON to 4D

4D Tech mailing list
In reply to this post by 4D Tech mailing list
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 <
[hidden email]> 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:[hidden email]
> **********************************************************************
>



--
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:[hidden email]
**********************************************************************
Reply | Threaded
Open this post in threaded view
|

Re: Question about passing JSON to 4D

4D Tech mailing list
have you tried...

C_OBJECT($body)
WEB GET HTTP BODY($body)

?

it might work if you are lucky...

2017/04/05 23:31、Kirk Brooks via 4D_Tech <[hidden email]<mailto:[hidden email]>> のメール:
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.


**********************************************************************
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:[hidden email]
**********************************************************************
Reply | Threaded
Open this post in threaded view
|

Re: Question about passing JSON to 4D

4D Tech mailing list
On Wed, Apr 5, 2017 at 7:40 AM, Keisuke Miyako via 4D_Tech <
[hidden email]> wrote:

> have you tried...
>
> C_OBJECT($body)
> WEB GET HTTP BODY($body)
> ?
> it might work if you are lucky...
>

I did and I'm not. ​In this case it looks like it's undocumented because it
doesn't work. In v15 anyway.

​But it's really not a big deal do simply parse the text. Thanks again for
getting me oriented with this. ​

--
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:[hidden email]
**********************************************************************