HELP: CFC+jquery+JSON

Ok, I've been at this for a couple of hours now. I thought I was on the right track, but it looks like I'm still a bit lost :(

I have a modal login form using jquery and colorbox plugin.

Here is a snippet of my jquery ajax call:


$j.ajax({
url: 'cfc/security.cfc?method=login',
data: datastring,
dataType: 'json',
success: function(data) {
    var response = eval(data);
    console.log(response.MESSAGE);
$j('.ajaxMsg').css({"display":"block"}).html(data);
$j.fn.colorbox.resize();
}
});

When calling this CFC from a URL using the correct parameters, I get this:
{"MESSAGE":"Verified! Logging in...","FNAME":"John","LNAME":"Smith","VALID":true}
This is exactly what I want.

However, when running the ajax call above I get:


<wddxPacket version='1.0'><header/><data><string>{"MESSAGE":"Verified! Logging in....","FNAME":"John","LNAME":"Smith","VALID":true}</string></data></wddxPacket>


And this is screwing me up.

Here is my CFC snippet:


<CFFUNCTION name="login" access="remote" returntype="string" output="false">
<cfargument name="username" type="string" required="Yes">
<cfargument name="password" type="string" required="Yes">
<cfargument name="stayLogged" type="string" required="no" default="no">
    
<CFSET myReturn = {}>
.... sql statements ...
if valid
<cfset myReturn.valid = "true">
<cfset myReturn.fname = #getLogin.fname#>
<cfset myReturn.lname = #getLogin.lname#>
<cfset myReturn.message = "Verified! Logging in....">

if invalid
<cfset myReturn.valid = "false">
<cfset myReturn.fname = "">
<cfset myReturn.lname = "">
<cfset myReturn.message = "Unverified! Please try signing in again.">

<cfset myReturn = serializeJSON(myReturn)>
    
<cfreturn myReturn>
</cffunction>

Ok...what am I doing wrong? :(

Comments
Admin's Gravatar HA! Only 2mins after posting this did I figure out the issue :)

change the URL parameter in the AJAX call

from:
url: 'cfc/security.cfc?method=login',

to:
url: 'cfc/security.cfc?method=login&returnformat=json',


Remove console.log and then change:
$j('.ajaxMsg').css({"display":"block"}).html(data);

to:
$j('.ajaxMsg').css({"display":"block"}).html(response.MESSAGE);
# Posted By Admin | 3/25/10 2:09 PM
Admin's Gravatar oh...I'm using '$j' instead of '$' because as a no conflict variable between jquery and prototype.

I'm trying to remove/replace all prototype/scriptaculous from this project (it's a makeover from a 2006 website that's been sitting in the dark for a while).
# Posted By Admin | 3/25/10 2:11 PM
Robert Zehnder's Gravatar You could also add returnFormat="JSON" to your CFFunction settings and negate the need to append it to the URL, especially if you are always going to be returning JSON from the method.
# Posted By Robert Zehnder | 3/25/10 3:02 PM
Admin's Gravatar @Robert: I tried that, but it still didn't work for me. Maybe I was doing something wrong elsewhere. Anyway, yeah if it's always going to return JSON, that's a good way to architect it.
# Posted By Admin | 3/25/10 3:04 PM
Ben Nadel's Gravatar I get the whole returnFormat issue; but, *why* were you getting a different response when hitting the URL?

Hmm, maybe you were still getting WDDX, but the browser was simply not rendering the WDDX tags (but trying to evaluate them as invalid HTML tags). Try hitting it via URL and viewing the page source.
# Posted By Ben Nadel | 3/29/10 12:57 PM
Robert Zehnder's Gravatar Well, it does seem a little strange to have returnFormat="JSON" with myReturn = serializeJSON(myReturn)
# Posted By Robert Zehnder | 3/29/10 1:03 PM
Admin's Gravatar @Ben: Yes you are right. Viewing source, I do see the WDDX wrapper.
# Posted By Admin | 3/29/10 1:33 PM
Admin's Gravatar Ok..I changed this line:
<CFFUNCTION name="login" access="remote" returntype="string" output="false">
to
<CFFUNCTION name="login" access="remote" returntype="any" output="false">

I removed this line:
<cfset myReturn = serializeJSON(myReturn)>

And it still works :)
# Posted By Admin | 3/29/10 1:40 PM
Ben Nadel's Gravatar Yeah, I've run into that problem (hidden WDDX tags) so many times :) Gets me like every time!
# Posted By Ben Nadel | 3/29/10 1:44 PM
Phillip Senn's Gravatar I don't see how you're passing in the parameters: username, password and staylogged.
It must have something to do with datastring.
# Posted By Phillip Senn | 3/29/10 4:31 PM
Admin's Gravatar @Phillip: Sorry, I missed this line:

var datastring = $j('#loginForm').serialize();
# Posted By Admin | 3/29/10 4:57 PM
Phillip Senn's Gravatar When I look at the response back in Firebug, it looks like I'm getting all the debug info in addition to my return code.
Here's just a part of what I see: (of course, the only thing I want to see is: 101):

101
</td></td></td></th></th></th></tr></tr></tr></table></table></table></a></abbrev></acronym></address></applet></au></b></banner></big></blink></blockquote></bq></caption></center></cite></code></comment></del></dfn></dir></div></div></dl></em></fig></fn></font></form></frame></frameset></h1></h2></h3></h4></h5></h6></head></i></ins></kbd></listing></map></marquee></menu></multicol></nobr></noframes></noscript></note></ol></p></param></person></plaintext></pre></q></s></samp></script></select></small></strike></strong></sub></sup></table></td></textarea></th></title></tr></tt></u></ul></var></wbr></xmp>
<style type="text/css">
.cfdebug
{
   color:black;
   background-color:white;
   font-family:"Times New Roman", Times, serif;
   font-size:small
}

.cfdebuglge
{
   color:black;
   background-color:white;
   font-family:"Times New Roman", Times, serif;
   font-size:medium;
}

a.cfdebuglink {color:blue; background-color:white }
</style>
<table class="cfdebug" bgcolor="white">
<tr>
   <td>

      <p class="cfdebug"><hr/>
      <b class="cfdebuglge"><a name="cfdebug_top">Debugging Information</a></b>
# Posted By Phillip Senn | 3/30/10 12:54 PM
Admin's Gravatar Hey Phillip...I'm no pro like Ben or Ray by any stretch, but if you could send me some of your code and what you're looking to do, maybe I can help you out.
# Posted By Admin | 3/30/10 12:59 PM

Calendar

NAVIGATION

Recent Entries

Recent Comments

RSS

Search

Subscribe

Tags