[英]jquery postback using c# and asp.net returns null
我正在嘗試使用jquery在單擊按鈕時調用ac#函數。 發生的情況是返回變量(msg)為null。
按鈕代碼:
<button id="test1" runat="server">Get Text</button>
JQuery代碼:
$(document).ready(function() {
$("#test1").click(function() {
$.ajax({
type: "POST",
url: "ServiceDirectoryAdd.aspx/GetCurrentDate",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
alert(msg);
}
});
});
});
C#函數:
[WebMethod]
public static string GetCurrentDate()
{
return "foo";
}
如我所說,返回變量msg返回null。 我做錯了什么嗎?
編輯:在C#函數中放置一個斷點后,似乎該程序沒有進入該函數。
您嘗試過GET
嗎?
$.get("ServiceDirectoryAdd.aspx/GetCurrentDate", function(data){
alert(data || data.d);
});
並編輯webmethod
以接受get
[WebMethod]
[ScriptMethod(UseHttpGet=true)]
public static string GetCurrentDate()
{
return "foo";
}
嘗試索引味精
alert(msg[0]); or alert(msg.d);
您是否將腳本管理器添加到頁面。 如果未添加。 請添加它,然后將腳本管理器的“ EnablePageMethod”屬性設置為true。
這是鏈接。 http://encosia.com/using-jquery-to-direct-call-aspnet-ajax-page-methods/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.