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

getJSON request using ColdFusion proxy

Enthusiast ,
Feb 08, 2016 Feb 08, 2016

Copy link to clipboard

Copied

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#" />

Views

929

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
community guidelines

correct answers 1 Correct answer

Enthusiast , Feb 09, 2016 Feb 09, 2016

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"

...

Votes

Translate

Translate
Enthusiast ,
Feb 08, 2016 Feb 08, 2016

Copy link to clipboard

Copied

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

Votes

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
community guidelines
Enthusiast ,
Feb 08, 2016 Feb 08, 2016

Copy link to clipboard

Copied

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.

Votes

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
community guidelines
Enthusiast ,
Feb 08, 2016 Feb 08, 2016

Copy link to clipboard

Copied

Looking at the Network tab/Response, I got a 404 not found on the following URL.

"NetworkError: 404 Not Found - https://myserver.mysite.com/myFolder/customcf/knowledgeOwl/ajaxProxy-KB.cfm?proxyURL=https%3A%2F%2Fa..."

Votes

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
community guidelines
Enthusiast ,
Feb 08, 2016 Feb 08, 2016

Copy link to clipboard

Copied

Okay, I got the 404 error resolved but now I have this one in the Nework/header.

HTTP/1.1 500 Complex object types cannot be converted to simple values.

Content-Type: text/html

Server: Microsoft-IIS/8.5

server-error: true

Date: Mon, 08 Feb 2016 19:37:34 GMT

Content-Length: 5141

Votes

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
community guidelines
Guide ,
Feb 08, 2016 Feb 08, 2016

Copy link to clipboard

Copied

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

Votes

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
community guidelines
Enthusiast ,
Feb 09, 2016 Feb 09, 2016

Copy link to clipboard

Copied

LATEST

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>

Votes

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
community guidelines
Resources
Documentation