如何使用 jquery 中的 cfc 返回的结构

发布于 2024-08-18 18:10:19 字数 2999 浏览 6 评论 0原文

我有一个 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>

显然 cfc 还有更多内容,但我似乎无法从结构中返回错误。
我正在使用这个 jquery 语句:

$("#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);
                  }
              });

        });

但是在 success 语句中我不确定如何获取从 cfc 返回的结构。我想我可以调用 msg.error 并获取信息,但我不能。我在 fireox 中使用 firebug,我可以看到它正在发出 POST 请求,但响应完全是空的。我不知道这是否会有所作为,但形式如下:

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

非常感谢任何帮助, 槊

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

卷耳 2024-08-25 18:10:19

首先,使用JSON(默认是WDDX,死定了)!有两种方法可以做到这一点。要么在 cffunction 中添加 returnformat="json" ,要么在 $.ajax(url="...?returnformat=json" 中添加 returnformat="json"

那么应该是使用 jQuery $.post 或 $.ajax 的问题,并设置回调函数对了。

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.

同展鸳鸯锦 2024-08-25 18:10:19

我会在带有 id 的输入周围使用表单标签。

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

确保您已关闭或抑制该 CFC 的 Coldfusion 调试。调试会破坏返回的 json 数据。

另外,创建一个标准 CFM 页面,初始化 cfc,然后 cfdump 调用页面正文中的函数。确保您对输出感到满意。如果结构体有任何错误,这种方式比使用 jQuery 和 firebug 更容易调试。一旦它工作,然后从 jQuery 调用它。

您可以简化 jQuery 语句:

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

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

jQuery 的 .serialize() 方法将极大地简化您的 javascript,因为您已将表单变量命名为与 url 参数相同。

请记住,javascript 区分大小写,而 Coldfusion 则不区分大小写。我相信 Coldfusion 通过远程返回所有大写字母。我认为您将“ERROR”全部大写是正确的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文