1
如何從Ajax命令獲取WebMethod?從Ajax命令獲取WebMethod
當我點擊Button1的我得到警報的錯誤代碼說:
"Error Code: [object Object]"
的Default.aspx:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
function GetDateTime() {
var jsonid = $("#<%= TextBox2.ClientID %>").val();
var json1 = "{'id':'" + jsonid + "'}";
$.ajax
({
type: "POST",
url: "Default.aspx/GetServerDateTime",
data: json1,
contentType: "application/json;charset=utf-8",
dataType: "json",
success: function (result) {
alert(result.d);
},
error: function (err) {
alert("Error Code: " + err);
}
});
}
</script>
Default.aspx.cs:
protected void Button1_Click(object sender, EventArgs e)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "scr", "Javascript:GetDateTime();", true);
}
[System.Web.Services.WebMethod]
public static string GetServerDateTime(string id)
{
string datetimeid1 = "ID: " + id + " Date&Time" + DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss tt");
return datetimeid1;
}