text.ascx CODE:
<script type = "text/javascript">
function ShowCurrentTime() {
$.ajax({
type: "POST",
url: "TestAjax.aspx/GetCurrentTime",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
}
});
}
function OnSuccess(response) {
alert(response.d);
}
<input type="checkbox" id='chkl' onclick=ShowCurrentTime();>';
text.ascx.cs 代码 :
[System.Web.Services.WebMethod]
public static string GetCurrentTime(string name)
{
return "Hello " + name + Environment.NewLine + "The Current Time is: "
+ DateTime.Now.ToString();
}
如何在用户控制中调用 jquery ajax。
不能在 UserControl 中调用 WebMethod。尝试在网页中使用WebMethod。
看看 已经在这里回答了
你能这样试试吗:
$.ajax({
type: "POST",
url: "*name.ascx/method",
data: { } ,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
var URL = msg.d;
alert(URL);
}
)};
希望这有帮助..
你可以试试这段代码。
$.ajax({
type: "POST",
url: "/yourURL",
dataType: "json",
data: "{}",
contentType: "application/json; charset=utf-8",
success: function(data) {
edata = $(data).find("string").text();
alert(edata);
},
error: function(e){
alert('err');
}
})