Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

CFC handling jQuery.serializeArray() data via AJaX post

LEGEND ,
Dec 29, 2017 Dec 29, 2017

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,

^ _ ^

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

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,

^ _ ^

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 31, 2017 Dec 31, 2017
LATEST

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.

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