在我的骨干项目中,我试图根据用户从表单提交的一些搜索条件来获取模型。在提交处理程序中,我试图通过传递搜索条件的数据选项来获取模型(以下是代码)。
var productType=$("#productType").val();
var customerId=$("#customerId").val();
var stateSelected=$("#selectedState").val();
var srbStatus=$("#stateReportingStatus").val();
var dateType=$("#dateType").val();
var fromDate=$("#fromDate").val();
var toDate=$("#toDate").val();
var billTypeInd=$("#billTypeIndicator").val();
var dataElement=$("#dataElement").val();
var ediFileName=$("#ediFileName").val();
var ediBillName=$("#ediBillNumber").val();
var billId=$("#billId").val();
var claimantFirstName=$("#claimantFirstName").val();
var claimantLastName=$("#claimantLastName").val();
var insurerName=$("#insurerName").val();
var insurerFEIN=$("#insurerFEIN").val();
var insurerZip=$("#insurerZIP").val();
var dashboardSearchResultModel= new dashboardSearchResult();
var dashboardSearchResultModel= new dashboardSearchResult();
dashboardSearchResultModel.fetch({
data:{
productType: productType,
customerId: customerId,
state:stateSelected,
srbStatus:srbStatus,
dateType:dateType,
fromDate:fromDate,
toDate:toDate,
billTypeInd:billTypeInd,
dataElement:dataElement,
ediFileName:ediFileName,
ediBillName:ediBillName,
billId:billId,
claimantFirstName:claimantFirstName,
claimantLastName:claimantLastName,
insurerName:insurerName,
insurerFEIN:insurerFEIN,
insurerZip:insurerZip
},
wait:true,
success: function(dashboardSearchResultModel)
{
alert("This is what we get for result"+JSON.stringify(dashboardSearchResultModel));
$('#dashboardResultArea').html(self.dashboardResultTemplate({results:dashboardSearchResultModel.get("results")}));
},
error: function (model, xhr, options) {
alert("Error: An error occurred while trying to fetch the dashboardSearchResultModel");
alert("Error got model"+JSON.stringify(model));
alert("options:"+JSON.stringify(options));
alert("xhr:"+JSON.stringify(xhr));
}
});
最初在提供搜索条件后加载页面时,如果我单击提交,则提取不起作用并转到错误处理程序。之后,当我从第二次提交时,fetch工作并从后端服务器检索数据。知道什么是错的吗?提前谢谢。
答案 0 :(得分:0)
调用错误回调时,这是因为您对服务器的XHTMLRequest返回了错误(HTTP状态代码)。所以,你的问题存在于哪里。
谁开始使用此代码?由于错误不会在第二次尝试时发生,我建议您在DOM未准备好时将callind $('#id')。val()区域化。这样,您将向服务器发送空值,这会导致您收到错误。
要解决您的问题,请确保在执行此脚本时DOM已准备就绪。 查看您的请求是否离开浏览器并到达服务器(即,跨域请求失败,状态为0,未到达服务器)。
如果是,请调试服务器端,因为它似乎不是客户端问题。
答案 1 :(得分:0)
所以在尝试了很多东西之后我终于决定尝试$ .ajax调用而不是fetch方法。这就是我想出来的
$.ajax({
type: "GET",
url: "rest/dashboardResult",
dataTyp: 'json',
data: {
productType: productType,
customerId: customerId,
state:stateSelected,
srbStatus:srbStatus,
dateType:dateType,
fromDate:fromDate,
toDate:toDate,
billTypeInd:billTypeInd,
dataElement:dataElement,
ediFileName:ediFileName,
ediBillName:ediBillName,
billId:billId,
claimantFirstName:claimantFirstName,
claimantLastName:claimantLastName,
insurerName:insurerName,
insurerFEIN:insurerFEIN,
insurerZip:insurerZip
}
})
.done(function(response) {
alert( "Result is: " + response);
});
这一切都没有任何问题。现在我的问题是如何将响应绑定到主干模型?
答案 2 :(得分:0)
最后我弄清楚出了什么问题。调用是在提交点击处理程序内,$ .ajax调用或提取是异步的。因此,当呼叫从服务器获得回复时,提交单击的默认操作已经发生(即重新加载页面)。所以当成功或.done被调用时,整个页面被重新加载。所以我将event.preventDefault()放在handler方法的开头,让处理程序从服务器接收回调并在模板中显示它。谢谢大家的时间。