[英]Asp.net jquery ajax call
我正在練習jquery ajax調用。 當我運行我的項目時,會發生內部服務器錯誤500的情況。該錯誤是什么意思,我怎么會遇到此錯誤?
我創建了一個示例項目,在其中添加了以下代碼:
namespace sample
{
public partial class jqAjaxcall : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
GetDateTime();
}
[WebMethod]
private static string GetDateTime()
{
return DateTime.Now.ToString();
}
}
}
我的aspx代碼文件:
<script type="text/javascript" src="Library/js/jquery-1.9.1-min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
// Add the page method call as an onclick handler for the div.
$("#Result").click(function () {
$.ajax({
type: "POST",
url: "jqAjaxcall.aspx/GetDateTime",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
// Replace the div's content with the page method's return.
$("#Result").text(msg.d);
}
});
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="Result">
Click here for the time.</div>
</form>
</body>
</html>
定義您的函數Public ...以便像這樣從Jquery訪問
[WebMethod]
//define function as public....
public static string GetDateTime()
{
return DateTime.Now.ToString();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.