如何使用 jquery 中的 cfc 返回的结构
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,使用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.
我会在带有 id 的输入周围使用表单标签。
确保您已关闭或抑制该 CFC 的 Coldfusion 调试。调试会破坏返回的 json 数据。
另外,创建一个标准 CFM 页面,初始化 cfc,然后 cfdump 调用页面正文中的函数。确保您对输出感到满意。如果结构体有任何错误,这种方式比使用 jQuery 和 firebug 更容易调试。一旦它工作,然后从 jQuery 调用它。
您可以简化 jQuery 语句:
jQuery 的 .serialize() 方法将极大地简化您的 javascript,因为您已将表单变量命名为与 url 参数相同。
请记住,javascript 区分大小写,而 Coldfusion 则不区分大小写。我相信 Coldfusion 通过远程返回所有大写字母。我认为您将“ERROR”全部大写是正确的。
I'd use a form tag around the inputs with an id.
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:
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.