<script src="jquery-1.3.2.min.js" type="text/javascript"></script>
<script type = "text/javascript">
function ShowCurrentTime() {
$.ajax({
type: "POST",
url: "Default.aspx/jsonrep",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
cache: false,
success: OnSuccess,
failure: function (response) {
alert(response.d);
}
});
}
function OnSuccess(response) {
alert(response.d);
}
</script>
<input id="btnGetTime" type="button" value="Show Current Time"
onclick = "ShowCurrentTime()" />
和代码文件代码是
[System.Web.Services.WebMethod]
public string jsonrep()
{
GetOrders();
var jsonSerialiser = new JavaScriptSerializer();
var json = jsonSerialiser.Serialize(list);
return json.ToString();
}
public List<DataProp> GetOrders()
{
DataProp o = new DataProp();
for (int i = 1; i <= 8; i++)
{
o.Earning = 20000 * i;
o.Number = 8 * i;
list.Add(o);
}
return list;
}
我想使用 jQuery 调用服务器端函数,该函数返回一个 json 对象但函数不起作用 - 我做错了什么?如果我在页面加载时调用函数,它工作正常是该函数返回一个 json 数据。请帮忙