[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

syntax and how to help

Posted on 2014-08-20
17
Medium Priority
?
249 Views
Last Modified: 2014-08-29
Part 1
We have a js function that loads a colorbox sending an object (qstr) with the url to the href setting
heres a simple ex:
qstr={abc:105487,xyz:18199};
href: '/testing.cfm?qstr=' + qstr

Part 2
now on testing.cfm from the above if i dump url.qstr it says it's [object Object] but cant figure out how to parse through it.
when trying to use $.each i get the "cannot use 'in' operator to search for ..." error

insight is needed ...
0
Comment
Question by:dgrafx
  • 6
  • 6
  • 4
16 Comments
 
LVL 43

Expert Comment

by:Rob
ID: 40275805
now on testing.cfm from the above if i dump url.qstr it says it's [object Object] but cant figure out how to parse through it.
when trying to use $.each i get the "cannot use 'in' operator to search for ..." error
Can you please show your code for this?  testing.cfm is run on the server, while the $.each is run on the client
0
 
LVL 25

Author Comment

by:dgrafx
ID: 40276505
hi Rob - there really isn't any CF code which is why I didn't include the CF zone
the only CF is where I turn url.qstr back into a js object

qstr = '#url.qstr#';

insight?
0
 
LVL 43

Expert Comment

by:Rob
ID: 40276515
Have you tried console.dir(your object)?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 43

Expert Comment

by:Rob
ID: 40276529
If you could site the $.each code you used, that would help.
Was it just

$.each(qstr, function(i,el) {
    console.dir(el);
});
0
 
LVL 25

Author Comment

by:dgrafx
ID: 40276592
yes i used similar
$.each(qstr, function(i,el) {
    console.log(el);
});

i also used console.log(qstr) which equals [object Object] but no option to expand to see it's contents so am wondering if it really is an object?

i don't need to do this exactly like I've shown so if you have a better method of sending the original object - which will be something like {abc:105487,xyz:18199} - through the url and parsing through it on the landing page.
I'm leaving right now for work and can post more when I get there so forgive the delay if you post back ...
0
 
LVL 52

Expert Comment

by:_agx_
ID: 40277032
> qstr = '#url.qstr#';

@dgrafx - Quick thought.  I don't think it is an object (from javascript's POV). Due to the surrounding quotes, it looks like it's assigning a simple string. That's why an error occurs when trying to loop through it.

ie  
 // assigns string
       var qstr = '{abc:105487,xyz:18199}';

       // assigns object
       var qstr = {abc:105487,xyz:18199};

Open in new window

0
 
LVL 25

Author Comment

by:dgrafx
ID: 40277067
right!

this is the thing
for testing if i code exactly this on the landing page:
var qstr = {abc:105487,xyz:18199};
$.each(qstr, function(i,el) {
    console.log(el);
});
then no problem - good to go

But what I need to do is send this "object" in the url to the landing page
Thus the issue as to what to do with #url.qstr#
i.e. how to loop through it just as if it was created like var qstr = {abc:105487,xyz:18199};

thanks
0
 
LVL 52

Expert Comment

by:_agx_
ID: 40277280
Can you change the format of URL.qstr, so it's valid JSON? Then run it through JSON.parse, and voila its' an object.

Other than that, the only thing I'm coming up with is to eval() the string, but ... that's not a great idea at all.
0
 
LVL 25

Author Comment

by:dgrafx
ID: 40277305
can you post an example?
and yes I can change the format

one of the first things i did was to try and send in valid json as in: {"abc":"10587","xyz":"18199"}
but still seems to convert to a string on landing page when translated i.e. #url.qstr#

thanks
0
 
LVL 52

Accepted Solution

by:
_agx_ earned 2000 total points
ID: 40277368
On the calling page, create a structure and serialize it as JSON. Be sure to url encode it before passing it to the target page:
      

    <cfset data = {}>
    <cfset data["abc"] = 123>
    <cfset data["efg"] = 456>
    <cfset url.qStr = serializeJSON(data)>
     
    Raw query string: <cfoutput>#url.qStr#</cfoutput>

Open in new window


On the target page, use jQuery.parseJSON to deserialize the string into an object.
 
   
    <html>
    <head> 
    <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
    <script type="text/javascript">
            <cfoutput>
            #toScript(url.qStr, 'qStr')#;
            </cfoutput>
            $( document ).ready(function() {
                    try {
                            $.each( jQuery.parseJSON(qStr), function( index, value ) {
                                    alert( index + ": " + value );
                            });
                    }
                    catch(e) {
                            alert(e.message);
                    }
            });    
    </script>
    </head>
    <body>
    </body>
    </html>

Open in new window

0
 
LVL 52

Expert Comment

by:_agx_
ID: 40277373
I think it'll work using a plain output:

      var qStr = '#url.qStr#';


... instead of this:

  <cfoutput>
      #toScript(url.qStr, 'qStr')#;
  </cfoutput>

but didn't actually test the former method.
0
 
LVL 25

Author Closing Comment

by:dgrafx
ID: 40277421
great and thanks!
btw it does work with var qstr = '#qstr#' as well as toString ...

the urlencodedformat was the clincher i believe!
thanks again!
0
 
LVL 52

Expert Comment

by:_agx_
ID: 40277464
Cool, glad it worked out.
0
 
LVL 43

Expert Comment

by:Rob
ID: 40277839
Thanks agx
0
 
LVL 25

Author Comment

by:dgrafx
ID: 40277851
thank you too Rob!
0
 
LVL 52

Expert Comment

by:_agx_
ID: 40294094
Welcome @Rob :)
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month20 days, 9 hours left to enroll

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question