1
讀完http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/之後,我專心親自嘗試。我現在想知道爲什麼我的ajax沒有被調用。非常感謝!jQuery Page方法
代碼背後
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();
}
}
和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>
你能發佈更多設置屬性
EnablePageMethods=true
在頁面的ScriptManager你的代碼?可能與任何「#Result」的問題應該選擇並附加點擊事件。 – fehays 2010-11-24 17:43:06