Skip to main content
2Charlie
Inspiring
February 8, 2016
Answered

getJSON request using ColdFusion proxy

  • February 8, 2016
  • 1 reply
  • 1368 views

I am following this article by Ben Nadel -- not sure if there are some other better tutorials out there. Anyway, I kept getting the Variable ALLARTICLESDATA is undefined. error.

<script type="text/javascript">

  // Run when DOM is ready.

        $(function(){

                var jForm = $( "form" );

                // Hook up form submit.

                jForm.submit(

                    function( objEvent ){

                        // Get articles from Knowledge Owl.

                        GetArticles();

                        // Prevent default.

                        return( false );

                    });

            });

  // This method actually performs the cross-site AJAX

        // using a proxy ColdFusion page.

        function GetArticles(){

            $('#searharea').keyup(function() {

     var searchField = $('#searharea').val();

     var myExp = new RegExp(searchField, "i");

     $.getJSON('ajaxProxy-KB.cfm', {proxyURL: 'https://app.knowledgeowl.com/api/head/article.json',

     name: {

           $regex: searchField,

           $options: "i"

       }},

      function(data) {

       //console.log( data.valid,  data.data); //  data.data is an array of information!

       var output = '<ul class="searchresults">';

       $.each(data.data, function(i, article) {

         output += '<li>';

         output += '<h5><a href="https://kb.mysite.com/help/pdfexport/id/'+ article.id + '">' + article.name + '</h5>';

         output += '</li>';

       });

       output += '</ul>';

       $('#resultarea').html(output);

     }); //get JSON

   });

        }

</script>

<cfoutput>

  <section class="block">

  <ul class="tabs">

     <li><a href="##tab1">Search</a></li>

     <li><a href="##tab2">Article</a></li>

     <li><a href="##tab3">Category</a></li>

    </ul>

    <article id="tab1">

  <input type="text" id="searharea" name="searcharea" placeholder="search article name"></input>

  <div id="resultarea"></div>

    </article>

    <article id="tab2">

  <cfif !IsJSON(allArticlesData)>

  <h3>The URL you requested does not provide valid JSON</h3>

  <cfelse> 

  <cfset allArtData=DeserializeJSON(allArticlesData)>

  <cfif structKeyExists(allArtData, 'data')>

          <cfloop index="i" from="1" to="#arrayLen(allArtData.data)#">      

             <h5><a href="https://kb.mysite.com/help/pdfexport/id/#allArtData.data.id#">#allArtData.data.name#</a></h5>

         </cfloop>

     </cfif>  

  </cfif>

     </article>

     <article id="tab3">

  <cfif !IsJSON(cateData)>

  <h3>The URL you requested does not provide valid JSON</h3>

  <cfelse> 

  <cfset cfDJData=DeserializeJSON(cateData)>

  <cfif structKeyExists(cfDJData, 'data')>

          <cfloop index="i" from="1" to="#arrayLen(cfDJData.data)#">      

             <h5>#cfDJData.data.name# - #cfDJData.data.id#</h5>

          </cfloop>

     </cfif>  

  </cfif>   

     </article>

    </section>

</cfoutput>

And here's my proxy page.

<!---

    Check to see if the page request is a POST or a GET.

    Based on this, we can figure out our target URL.

--->

<cfif (CGI.request_method EQ "get")>

    <!--- Get URL-based target url. --->

    <cfset strTargetURL = URL.ProxyURL />

    <!--- Delete target URL. --->

    <cfset StructDelete( URL, "ProxyURL" ) />

<cfelse>

    <!--- Get FORM-based target url. --->

    <cfset strTargetURL = FORM.ProxyURL />

        <!--- Delete target URL. --->

    <cfset StructDelete( FORM, "ProxyURL" ) />

</cfif>

<!---

    Remove any AJAX anit-caching that was used by jQuery. This

    is a random number meant to help ensure that GET URLs are

    not cached.

--->

<cfset StructDelete( URL, "_" ) />

<!---

    Make the proxy HTTP request using. When we do this, try to pass along all of the CGI information that was made by the original AJAX request.

--->

<cfhttp result="objRequest" url="#UrlDecode( strTargetURL )#" method="#CGI.request_method#" useragent="#CGI.http_user_agent#" timeout="15">

    <!--- Add the referer that was passed-in. --->

    <cfhttpparam type="header" name="referer" value="#CGI.http_referer#" />

    <!--- Pass along any URL values. --->

    <cfloop item="strKey" collection="#URL#">

        <cfhttpparam type="url" name="#LCase( strKey )#" value="#URL[ strKey ]#" />

    </cfloop>

  

   <!--- Pass along any FORM values. --->

    <!---<cfloop item="strKey" collection="#FORM#">

        <cfhttpparam type="formfield" name="#LCase( strKey )#" value="#FORM[ strKey ]#" />

    </cfloop>--->

  <cfhttpparam type="url" name="_authbykey" value="56a7d8c123131c4058361567">

    <cfhttpparam type="url" name="project_id" value="55c4ffd123131c567e294fe6">

    <cfhttpparam type="url" name="status[$in][]" value="published">

    <cfhttpparam type="url" name="status[$in][]" value="review">

    <cfhttpparam type="url" name="_fields[]" value="name">

</cfhttp>

<!---

<!--- Debug most current request. --->

<cfset objDebug = {

    CGI = Duplicate( CGI ),

    URL = Duplicate( URL ),

    FORM = Duplicate( FORM ),

    Request = Duplicate( objRequest )

    } />

<!--- Output debug to file. --->

<cfdump

    var="#objDebug#"

    output="#ExpandPath( './ajax_prox_debug.htm' )#"

    format="HTML"

    />

--->

<!---

    Get the content as a byte array (by converting it to binary,

    we can echo back the appropriate length as well as use it in

    the binary response stream.

--->

<cfset binResponse = ToBinary(ToBase64( objRequest.FileContent )) />

<!--- Echo back the response code. --->

<cfheader statuscode="#Val( objRequest.StatusCode )#" statustext="#ListRest( objRequest.StatusCode, ' ' )#" />

<!--- Echo back response legnth. --->

<cfheader name="content-length" value="#ArrayLen( binResponse )#" />

<!--- Echo back all response heaers. --->

<cfloop item="strKey" collection="#objRequest.ResponseHeader#">

<!--- Check to see if this header is a simple value. --->

    <cfif IsSimpleValue( objRequest.ResponseHeader[ strKey ] )>

        <!--- Echo back header value. --->

        <cfheader name="#strKey#" value="#objRequest.ResponseHeader[ strKey ]#" />

    </cfif>

</cfloop>

<!---

    Echo back content with the appropriate mime type. By using

    the Variable attribute, we will make sure that the content

    stream is reset and ONLY the given response will be returned.

--->

<cfcontent type="#objRequest.MimeType#" variable="#binResponse#" />

This topic has been closed for replies.
Correct answer 2Charlie

What is "Enable HTTP status codes" set to (checked/unchecked) in the Settings page of CF Administrator?


It turns out I don't need this cfloop block of code. The error is actually coming from the cfheader tag. It works fine without the cfloop at this moment so I'm going to just remove it.

<cfloop item="strKey" collection="#objRequest.ResponseHeader#">

<!--- Check to see if this header is a simple value. --->

    <cfif IsSimpleValue( objRequest.ResponseHeader[ strKey ] )>

        <!--- Echo back header value. The cfheader tag generate the error "Complex object types cannot be converted to simple values"--->

        <cfheader name="#strKey#" value="#objRequest.ResponseHeader[ strKey ]#" />

        <cflog text="IsSimpleValue: #strKey#" type="Information" file="debugCFLoop">

    </cfif>

</cfloop>

1 reply

2Charlie
2CharlieAuthor
Inspiring
February 8, 2016

I resolved the Variable ALLARTICLESDATA is undefined. error but the search is not working. There is nothing displaying or returned.

2Charlie
2CharlieAuthor
Inspiring
February 8, 2016

I think the issue is in the coldfusion proxy page (ajaxProxy-KB.cfm). Since in my page, did not do a submit (I only did a keyup event), I need to modify my CF-proxy page. If that's the case, I wonder if I even need to grab the proxyURL or not.

2Charlie
2CharlieAuthor
Inspiring
February 8, 2016