【发布时间】:2011-08-01 00:04:43
【问题描述】:
我正在使用 JSF 2.0 构建一个日程安排应用程序,用户可以在其中向日历添加项目,然后编辑这些对象。
我广泛使用 AJAX 来防止页面刷新。
我遇到的问题是从使用 AJAX 调用的函数中获取返回值。
<!-- ... form to fill -->
<h:commandButton value="Insert">
<f:ajax execute="formField1 formField2..."
listener="#{myBean.insert()}" onevent="processSave"/>
</h:commandButton>
这成功调用了我的 JavaScript 函数processSave()。
myBean.insert()返回数据库中新插入行的id
public String insert() {
//... inserting data into database
id = {id from database is obtained here}
System.out.println("I have the ID here : " id); //this works
return id;
}
我一直试图从 processSave() 函数的 JavaScript 中的响应对象中获取此信息。
processSave(data) {
if(data.status == "begin") {
// ation done here
} else if(data.status == "complete") {
// more actions done here
} else if(data.status == "success") {
//I am trying to get the ID here
//I've tried looking into data.responseXML, but to no avail.
}
}
我正在尝试使用当前技术吗?
我认为可以使用 AJAX 调用的 render 组件更新页面中的字段,然后使用 Javascript 获取值。但我认为这不会那么干净?
谢谢!
【问题讨论】:
标签: javascript ajax jsf-2