jQuery 页面方法
阅读 http:// encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/ 我决定自己尝试一下。我现在想知道为什么我的ajax没有被调用。非常感谢!
背后的代码
TestAjax.cs
using System;
using System.Collections.Generic;
//using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
public partial class TestingAjax : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static string GetDate()
{
return DateTime.Now.ToString();
}
}
和TestingAjax.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestingAjax.aspx.cs" Inherits="TestingAjax" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript" language = "javascript" >
$(document).ready(function () {
// Add the page method call as an onclick handler for the div.
$("#Result").click(function () {
alert("entre en el click:");
$.ajax({
type: "post",
url: "TestingAjax.aspx/GetDate",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
alert("Returning from Ajax");
// 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>
After reading http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/ I dediced to try it myself. I'm now wondering why my ajax is not being called. Many thanks!
code behind
TestingAjax.cs
using System;
using System.Collections.Generic;
//using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
public partial class TestingAjax : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static string GetDate()
{
return DateTime.Now.ToString();
}
}
and TestingAjax.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestingAjax.aspx.cs" Inherits="TestingAjax" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript" language = "javascript" >
$(document).ready(function () {
// Add the page method call as an onclick handler for the div.
$("#Result").click(function () {
alert("entre en el click:");
$.ajax({
type: "post",
url: "TestingAjax.aspx/GetDate",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
alert("Returning from Ajax");
// 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
记得在页面的 ScriptManager 上设置属性
EnablePageMethods=true
remember to set the property
EnablePageMethods=true
on the ScriptManager of the page