asp.net Ajax調用Aspx後臺方法
阿新 • • 發佈:2018-05-30
出錯 ESS CA console 如果 title eth set ring
Ajax調用的前提(以aspx文件為例:)
1、首先需要在aspx文件後臺中引用using System.Web.Services;
2、需要調用的方法必須是公共的(public)、靜態的(static);如果不是會提示“500 Internal Server Error 問題”,代表找不到method。
3、方法定義需要加入[WebMethod]的聲明
4、一般建議由返回類型,最起碼可能知道調用成功不成功。
下面是簡單的調用示例:
後臺方法
[WebMethod] public static string WriteLog(DateTime StartTime, DateTime EndTime)
{if (true) { return ""; } else { return "讀取出錯!"; } return ""; }
前臺調用
$.ajax({
url: ‘Index.aspx/WriteLog‘,
type: ‘post‘,
data: JSON2.stringify({ StartTime: $(‘#dtbStartTime‘).datetimebox("getValue"), EndTime: $(‘#dtbEndTime‘).datetimebox("getValue")}),
cache: false, dataType: ‘json‘,
contentType: "application/json;charset=utf-8",
success: function (data) {
console.info(data);
}
});
註意前臺調用時必須把json對象轉為json字符串,否則會報錯 “
無效的 JSON 基元: Types
”
asp.net Ajax調用Aspx後臺方法