Highlighted

CFC handling jQuery.serializeArray() data via AJaX post

LEGEND ,
Dec 29, 2017

Copy link to clipboard

Copied

Hello, all,

I've got a form that I'm using AJaX to submit after using jQuery.serializeArray() and JSON.stringify().  I've set the contentType to "application/json".  What ColdFusion gets in the CFC is an array of structs.  Screencap included.

<cfset requestBody = toString(getHttpRequestData().content) />

ArrayOfStructs.png

What I would _like_ to do is convert this to the form scope.  But converting to a query is just as good.  Question is:  What is the most efficient way to do this?

V/r,

^ _ ^

Views

346

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

CFC handling jQuery.serializeArray() data via AJaX post

LEGEND ,
Dec 29, 2017

Copy link to clipboard

Copied

Hello, all,

I've got a form that I'm using AJaX to submit after using jQuery.serializeArray() and JSON.stringify().  I've set the contentType to "application/json".  What ColdFusion gets in the CFC is an array of structs.  Screencap included.

<cfset requestBody = toString(getHttpRequestData().content) />

ArrayOfStructs.png

What I would _like_ to do is convert this to the form scope.  But converting to a query is just as good.  Question is:  What is the most efficient way to do this?

V/r,

^ _ ^

Views

347

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Dec 29, 2017 0
LEGEND ,
Dec 29, 2017

Copy link to clipboard

Copied

Well, I don't know if this is the most efficient way, but I did fashion a method to do it.  Feels kind of hackish.  If anyone else knows a better way, PLEASE, I am all about learning something new / fresh. 

In the CFC, at the top of the function that performs form validation/sanitization, I added the following (before a cftry tag):

<cfset form = {} />

<cfset requestBody = DeserializeJSON(toString(getHttpRequestData().content)) />

<cfset aryLen = ArrayLen(requestBody) />

<cfloop index="idx" from="1" to="#val(aryLen)#" step="1">

     <cfif isStruct(requestBody[idx]) AND Structcount(requestBody[idx])>

          <cfset form[requestBody[idx].name] = requestBody[idx].value />

     </cfif>

</cfloop>

<cfdump var="#form#" />

.. and it works.

V/r and HTH,

^ _ ^

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 29, 2017 0
BKBK LATEST
Adobe Community Professional ,
Dec 31, 2017

Copy link to clipboard

Copied

Fine. I would call it requestData instead of form, to avoid any possible confusion down the line. For example, the existence of the form scope usually implies the existence of a referrer.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 31, 2017 1