我从ajax调用调用web服务。 Web服务正在返回所有值。由于其中一个属性是datetime,我使用这个Js函数来解析日期。
function ToJavaScriptDate(value) {
var pattern = /Date\(([^)]+)\)/;
var results = pattern.exec(value);
var dt = new Date(parseFloat(results[1]));
return (dt.getMonth() + 1) + "/" + dt.getDate() + "/" + dt.getFullYear();
// return (dt.getFullYear() + "/" + (dt.getMonth() + 1) +"/" + dt.getDate());
}
我在这里呼叫网络服务
$(document).ready(function() {
$('#ShowInGridButton').on('click',
function() {
var accountNumberId = $('#DepositAccountNumberIdHiddenField').val();
var fromDate = $("input[name*='FromDateControl']").val();
var toDate = $("input[name*='ToDateControl']").val();
var url = "/WebMethods/Deposit.asmx/GetDepositAccountStatement";
var d = {
accountNumberId: accountNumberId,
fromDate: fromDate,
toDate: toDate
};
var jqXhr = ajaxPost(url, d);
jqXhr.done(function(data) {
console.log(data);
if (data) {
var div = $('#CfDataTable').empty();
div.append(
$("<tr class='success'><th>Tran Date</th><th>Value Date</th><th>Statement Reference</th><th>Check No.</th><th>Collector</th><th>Debit</th><th>Credit</th><th>Balance</th></tr>"));
$(data.d).each(function(index, item) {
div.append(
$("<tr>")
.append($("<td>").html(ToJavaScriptDate(item.TranDate)))
.append($("<td>").html(ToJavaScriptDate(item.ValueDate)))
// .append($("<td>").html(item.TranDate))
// .append($("<td>").html(item.ValueDate))
.append($("<td>").html(item.StatementReference))
.append($("<td>").html(item.CheckNumber))
.append($("<td>").html(item.Collector))
.append($("<td>").html(item.Debit))
.append($("<td>").html(item.Credit))
.append($("<td>").html(item.Balance)));
});
}
}).fail(function() {
console.log('Data Retrieve failed.');
});
});
});
XML输出
<ArrayOfDepositAccountStatementView xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://Everest.Net.Web.WebMethods/">
<DepositAccountStatementView>
<TranId>13018256</TranId>
<TranDate>2016-01-14T00:00:00</TranDate>
<ValueDate>2016-01-14T00:00:00</ValueDate>
<Collector>Office</Collector>
<Debit xsi:nil="true" />
<Credit>11000</Credit>
<Balance>11000</Balance>
</DepositAccountStatementView>
<DepositAccountStatementView>
<TranId>23601</TranId>
<TranDate>2016-02-04T00:00:00</TranDate>
<ValueDate>2016-02-04T00:00:00</ValueDate>
<StatementReference>Collected by Krishna Pokhrel</StatementReference>
<Collector>Krishna Pokhrel</Collector>
<Debit>0</Debit>
<Credit>1000</Credit>
<Balance>12000</Balance>
<CheckNumber/>
</DepositAccountStatementView>
<DepositAccountStatementView>
<TranId>69273</TranId>
<TranDate>2016-03-01T00:00:00</TranDate>
<ValueDate>2016-03-01T00:00:00</ValueDate>
<StatementReference>Collected by Krishna Pokhrel</StatementReference>
<Collector>Krishna Pokhrel</Collector>
<Debit>0</Debit>
<Credit>1000</Credit>
<Balance>13000</Balance>
<CheckNumber/>
</DepositAccountStatementView>
<DepositAccountStatementView>
<TranId>118061</TranId>
<TranDate>2016-04-01T00:00:00</TranDate>
<ValueDate>2016-04-01T00:00:00</ValueDate>
<StatementReference>Collected by Krishna Pokhrel</StatementReference>
<Collector>Krishna Pokhrel</Collector>
<Debit>0</Debit>
<Credit>1000</Credit>
<Balance>14000</Balance>
<CheckNumber/>
</DepositAccountStatementView>
<DepositAccountStatementView>
<TranId>180233</TranId>
<TranDate>2016-05-10T00:00:00</TranDate>
<ValueDate>2016-05-10T00:00:00</ValueDate>
<StatementReference>Collected by Krishna Pokhrel</StatementReference>
<Collector>Krishna Pokhrel</Collector>
<Debit>0</Debit>
<Credit>1000</Credit>
<Balance>15000</Balance>
<CheckNumber/>
</DepositAccountStatementView>
<DepositAccountStatementView>
<TranId>230251</TranId>
<TranDate>2016-06-08T00:00:00</TranDate>
<ValueDate>2016-06-08T00:00:00</ValueDate>
<StatementReference>Collected by Krishna Pokhrel</StatementReference>
<Collector>Krishna Pokhrel</Collector>
<Debit>0</Debit>
<Credit>1000</Credit>
<Balance>16000</Balance>
<CheckNumber/>
</DepositAccountStatementView>
<DepositAccountStatementView>
<TranId xsi:nil="true" />
<TranDate xsi:nil="true" />
<ValueDate xsi:nil="true" />
<StatementReference>Total</StatementReference>
<Debit>0</Debit>
<Credit>16000</Credit>
<Balance xsi:nil="true" />
</DepositAccountStatementView>
</ArrayOfDepositAccountStatementView>
一切都很好。但是当其中一行具有TranDate的空值时 和ValueDate null,我在附加的div中看不到任何数据。
如果我不使用上面的JS dateparser并且只是像item.TranDate一样追加我将tranDate的所有行作为TranDate:"/Date(1460398500000)/"
这里有什么问题?
我尝试了JSON.stringyfy(item.TranDate)
但无处可去。
在我的控制台中,我可以看到数据为
{__ type:“Everest.Net.Common.Deposit.Views.DepositAccountStatementView”,TranId:null,TranDate:null,ValueDate:null,StatementReference:“Total”
但这不会附加在div中。
答案 0 :(得分:0)
解决了这个..问题与我的ToJavaScriptDate函数有关。我必须在调用函数之前检查一个空值。
function ToJavaScriptDate(value) {
if(value !== null
&& value !== undefined
)
{
var pattern = /Date\(([^)]+)\)/;
var results = pattern.exec(value);
var dt = new Date(parseFloat(results[1]));
return (dt.getMonth() + 1) + "/" + dt.getDate() + "/" + dt.getFullYear();
// return (dt.getFullYear() + "/" + (dt.getMonth() + 1) +"/" + dt.getDate());
}
else
{
return '';
}
}
&#13;