views:

286

answers:

2

I have a cfc

<cffunction name="addEditPerson" access="remote" returntype="struct">
a bunch of cfarguments

<cfscript>
            var returnThis = structNew();
            var error = '';
            structInsert(returnThis,'success',0,true);
            structInsert(returnThis,'error','',true);
            structInsert(returnThis,'personID',arguments.personID,true);
            if (trim(arguments.fname) == ''){error=error&'<li>Enter a First Name</li>';}
            if (trim(arguments.lname) == ''){error=error&'<li>Enter a Last Name</li>';}
            if (len(trim(arguments.username)) lt 5){error=error&'<li>Enter a User Name (at least five(5) characters long)</li>';}
            if (trim(arguments.password) == ''){arguments.canLogin = false;}
            if (error != ''){
            structUpdate(returnThis,'error',error);
            return returnThis;
        }
</cfscript>

There is obviously more to the cfc but I can't seem to get the error to return from the structure.
I am using this jquery statement:

$("#addNewPerson").click(function(){
            var fName = $("#newPersonFname").val();
            var lName = $("#newPersonLname").val();
            var companyName = $("#newPersonCompanyName").val();
            var userName = $("#newPersonUserName").val();
            var roleID = $("#newPersonRole").val();
            var dataStr = 'fName='+fName+'&lName='+lName+'&companyName='+companyName+'&userName='+userName+'&roleID='+roleID;
              $.ajax({
                  type:"POST",
                  url:"/cfc/people.cfc?method=addEditPerson&returnformat=json",
                  data: dataStr,
                  cache:false,
                  success: function(msg) {
                  $("#newPersonError").html(msg.ERROR);
                  }
              });

        });

But in the success statement I am not sure how to get to the structure returned from the cfc. I would think I could call msg.error and get the info but I can't. I am using firebug in firedox and I can see that the POST request it being made but the response is complete empty. I don't know if it will make a difference but here is the form:

<div id="personForm">

    <div class="pageHeader">Add New Person</div>
    <div id="newPersonError"></div>
    First Name : <input id="newPersonFname" type="text" name="newPersonFname" value=""><br/>
    Last Name : <input id="newPersonLname" type="text" name="newPersonLname" value=""><br/>
    User Name : <input id="newPersonUserName" type="text" name="newPersonUserName" value=""><br/>
    Company Name : <input id="newPersonCompanyName" type="text" name="newPersonCompanyName" value=""><br/>
    Role : <select id="newPersonRole" name="newPersonRole">
                <option value="0">Select person's role<cfoutput query="roleList"><option value="#roleID#">#role#</cfoutput>
            </select><br/>
    <input id="addNewPerson" type="button" name="addPerson" value="Add New Person">
</div>

Any help is greatly apprieciated, Lance

+2  A: 

First, use JSON (default is WDDX, which is deader than dead)! There're two way to do it. Either add returnformat="json" in the cffunction, or in the $.ajax(url="...?returnformat=json"

Then it should be the matter of using jQuery $.post or $.ajax, and set up the call back function right.

Henry
I have added the in hope that it would display the string content of the structure but still no luck. Firebug is still showing no response at all (nor an error of any kind) and if I try to display alert(msg.ERROR) it is undefined. Thank again for your help
Lance
Maybe verify the method is running properly without any errors by invoking it in a .cfm page directly inside cfoutput?
Henry
Yup If I run <cfinvoke component="cfc.people" method="addEditPerson" returnvariable="result"></cfinvoke><cfdump var="#result#"> the dump shows the structure with everything as expected. I even tried changing the cffunction by changing "cfscript" to "fscript" to force cf to return an error. The error did show up in firebug so I know that the ajax call I am making is working correctly it is just my handling of the response that I seem to be screwing up. I am stumped
Lance
ok, try invoking directly in the browser in the URL bar, see if u see the resulting JSON string.
Henry
could this be a configuration problem? If I invoke from test.cfm it works but if I put it directly into the url (http://mydomain.com/cfc/people.cfc?method=addEditPerson) there is nothing?? If I change it to http://mydomain.com/cfc/people.cfc?method=addEditPerson2 still nothing if I put http://mydomain.com/cfc/people2.cfc?method=addEditPerson it DOES at least throw an error.
Lance
not related to this, but instead of using structInsert()/structUpdate(), just use returnThis.success=0; for example...
Henry
@Lance: If you typed it directly in the URL, did you view source? Is debugging on or off in the CF administrator?
Dan Sorensen
A: 

I'd use a form tag around the inputs with an id.

<div id="container">
<form id="personForm">
    ...
</form>
</div>

Make sure you have Coldfusion debugging turned off or suppressed for that CFC. Debugging will mangle the return json data.

Also, create a standard CFM page, init the cfc, and cfdump a call to the function in the body of the page. Make sure that you're happy with the output. If there are any errors with the struct, it will be easier to debug it this way than with jQuery and firebug. Once it is working, then call it from jQuery.

You can simplify your jQuery statement:

var peopleUrl = '/cfc/people.cfc?method=addEditPerson';

$.post(peopleUrl, $('#personForm').serialize(), function (msg) {
    $("#newPersonError").html(msg.ERROR);       
}, "json");

jQuery's .serialize() method will greatly simplify your javascript since you have named your form vars the same as your url params.

Remember that javascript is case sensitive whereas Coldfusion is not. I believe that Coldfusion returns everything upper case via remote. I think you have it correct by putting 'ERROR' in all caps.

Dan Sorensen
shouldn't the last argument to the $.post be "json"?
Henry
Could this be a configuation issue?? If I go to '/cfc/people.cfc' should it automatically bring up the cfide/componentutils component broswer? I think it should but it doesn't?
Lance
yes, sounds like a configuration issue...
Henry
@Henry: Yes, sorry, it should be JSON. I often just return an HTML message rather than a JSON struct.
Dan Sorensen
OK Dan and Henry, thanks for the help so far. I found that my application.cfc was blocking the ajax and fixed that. Now I am getting a string that looks like json {"success":0.0,"personID":0.0,"error":"<li>Enter a First Name<\/li><li>Enter a Last Name<\/li><li>Enter a User Name (at least five(5) characters long)<\/li><li>Select a role<\/li>"} but I cant get to msg.ERROR for some reason
Lance
Javascript is case sensitive. Based on your example, it looks looks like it's using lower case:...":0.0,"error":"<li>E...'error' is the var name. So you'll access it using 'msg.error'
Dan Sorensen
Likewise, msg.personID == 0.0 and msg.success == 0.0 in your example.
Dan Sorensen
That is what I would have thought and why I am so confused. if I do: alert(msg); I get {"success":0.0,"personID":0.0,"error":"<li>Enter a First Name<\/li><li>Enter a Last Name<\/li><li>Enter a User Name (at least five(5) characters long)<\/li><li>Select a role<\/li>"}but if I do alert(msg.error); I get "undefined" do I have to use parseJSON? or something like that?
Lance
in your $post, use , "json");
Henry
Agreed, I just updated my answer to include 'json' rather than 'html' as Henry pointed out. Although, if you do that, dump the result and verify the case again.
Dan Sorensen