我正在尝试使用ajax请求调用web服务。我正在寻找日期间隔(即2015-01-01,2016-06-08)之间的所有临床病例。单独测试时,Web服务工作得非常好。这是标题:
[WebMethod]
public void getCasesForDateInterval(string startDate, string endDate)
{
...
}
这是我应该从上述两个日期中检索的JSON文件:
[{"key":"SSB","values":[]},{"key":"GEN","values":[]},{"key":"LYM","values":[]},{"key":"LUD","values":[]},{"key":"GCC","values":[{"dateOpened":"2015-06-01","total":1},{"dateOpened":"2015-06-15","total":2},{"dateOpened":"2015-06-17","total":2},{"dateOpened":"2015-06-22","total":1},{"dateOpened":"2015-06-29","total":1},{"dateOpened":"2015-07-21","total":1},{"dateOpened":"2015-07-22","total":1},{"dateOpened":"2015-08-10","total":1},{"dateOpened":"2015-08-11","total":1},{"dateOpened":"2015-08-19","total":1},{"dateOpened":"2015-08-23","total":1},{"dateOpened":"2016-06-07","total":3}]},{"key":"MAC","values":[{"dateOpened":"2016-06-07","total":1}]},{"key":"MMB","values":[{"dateOpened":"2015-01-02","total":2},{"dateOpened":"2015-01-05","total":1},{"dateOpened":"2015-01-06","total":4},{"dateOpened":"2015-01-07","total":1},{"dateOpened":"2015-01-08","total":2},{"dateOpened":"2015-01-09","total":1},{"dateOpened":"2015-01-12","total":2},{"dateOpened":"2015-01-13","total":6},{"dateOpened":"2015-01-14","total":3},{"dateOpened":"2015-01-15","total":3},{"dateOpened":"2015-01-16","total":2},{"dateOpened":"2015-01-19","total":7},{"dateOpened":"2015-01-20","total":4},{"dateOpened":"2015-01-21","total":1},{"dateOpened":"2015-01-22","total":3},{"dateOpened":"2015-01-23","total":4},{"dateOpened":"2015-01-26","total":3},{"dateOpened":"2015-01-27","total":5},{"dateOpened":"2015-01-28","total":3},{"dateOpened":"2015-01-29","total":3},{"dateOpened":"2015-01-30","total":6},{"dateOpened":"2015-02-03","total":8},{"dateOpened":"2015-02-04","total":10},{"dateOpened":"2015-02-05","total":8},{"dateOpened":"2015-02-06","total":17},{"dateOpened":"2015-02-07","total":1},{"dateOpened":"2015-02-08","total":2},{"dateOpened":"2015-02-09","total":3},{"dateOpened":"2015-02-10","total":2},{"dateOpened":"2015-02-12","total":5},{"dateOpened":"2015-02-13","total":3},{"dateOpened":"2015-02-17","total":6},{"dateOpened":"2015-02-18","total":4},{"dateOpened":"2015-02-19","total":5},{"dateOpened":"2015-02-20","total":2},{"dateOpened":"2015-02-22","total":1},{"dateOpened":"2015-02-24","total":2},{"dateOpened":"2015-02-25","total":2},{"dateOpened":"2015-02-27","total":8},{"dateOpened":"2015-03-03","total":7},{"dateOpened":"2015-03-04","total":4},{"dateOpened":"2015-03-05","total":2},{"dateOpened":"2015-03-06","total":5},{"dateOpened":"2015-03-08","total":1},{"dateOpened":"2015-03-09","total":3},{"dateOpened":"2015-03-10","total":4},{"dateOpened":"2015-03-11","total":6},{"dateOpened":"2015-03-12","total":3},{"dateOpened":"2015-03-13","total":2},{"dateOpened":"2015-03-15","total":1},{"dateOpened":"2015-03-17","total":5},{"dateOpened":"2015-03-18","total":2},{"dateOpened":"2015-03-19","total":1},{"dateOpened":"2015-03-20","total":14},{"dateOpened":"2015-03-22","total":1},{"dateOpened":"2015-03-23","total":1},{"dateOpened":"2015-03-24","total":4},{"dateOpened":"2015-03-25","total":2},{"dateOpened":"2015-03-26","total":8},{"dateOpened":"2015-03-27","total":13},{"dateOpened":"2015-03-28","total":1},{"dateOpened":"2015-03-29","total":1},{"dateOpened":"2015-04-10","total":1},{"dateOpened":"2015-04-24","total":2},{"dateOpened":"2015-07-08","total":1},{"dateOpened":"2016-03-15","total":1},{"dateOpened":"2016-03-25","total":1},{"dateOpened":"2016-06-07","total":3}]}]
但是,当我尝试获取此JSON文件并将其发布到div元素中时,它无效:
<script type="text/javascript">
$(document).ready(function () {
$(document).on("click", "#btGO", function () {
var startDate = $("#startDate").val();
var endDate = $("#endDate").val();
var resultElement = $("#resultDiv");
$.ajax({
url: "dataWebService.asmx/getCasesForDateInterval",
method: "get",
data: {startDate: startDate, endDate: endDate},
dataType: "json",
success: function (data) {
resultElement.html(data);
}
});
});
});
</script>
当我更换&#34;得到&#34; by&#34; post&#34;并使用警报(即警报(数据)),我明白了:
这是否意味着我的ajax请求有效,但返回的JSON文件是空的?我是JQuery和ajax的新手。我觉得我犯了一个明显的错误。我非常感谢社区的反馈。谢谢!
答案 0 :(得分:0)
在调用您的网络方法时尝试使用此代码:
var seconds = '101.2914';
var date = new Date(0,0,0);
date.setSeconds(seconds);
var result = date.getMinutes() +':'+date.getSeconds()+'.'+seconds.split('.')[1].slice(0,3);