如何使ajax功能起作用 [英] How do I make a ajax function work
本文介绍了如何使ajax功能起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使ajax功能正常工作?
错误代码:
How do I make a ajax function work?
Error code:
JavaScript runtime error: '$' is undefined
Default.aspx
Default.aspx
<script type="text/javascript">
function GetDateTime(json1) {
$.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) {
}
});
}
</script>
<asp:Button ID="Button1" runat="server" Text="AJAX" OnClick="Button1_Click" />
Default.aspx.cs
Default.aspx.cs
protected void Button1_Click(object sender, EventArgs e)
{
string json1 = "{'one':'1','two':'2','three':'3'}";
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "scr", "Javascript:GetDateTime(" + json1 + ");", true);
}
[WebMethod]
public static string GetServerDateTime(string one, string two, string three)
{
return DateTime.Now.ToString() + " " + one + two + three;
}
推荐答案
'未定义
Default.aspx
Default.aspx
<script type="text/javascript">
function GetDateTime(json1) {
.ajax
({
type: POST,
url: Default.aspx / GetServerDateTime,
data:json1,
contentType: application / json; charset = utf-8,
dataTyp e: json,
成功:功能(结果){
alert(result.d);
},
错误:功能(错误){
}
});
}
< / 脚本 >
< asp:按钮 ID = Button1 runat = server 文本 = AJAX OnClick = Button1_Click / >
.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) { } }); } </script> <asp:Button ID="Button1" runat="server" Text="AJAX" OnClick="Button1_Click" />
Default.aspx.cs
Default.aspx.cs
protected void Button1_Click(object sender, EventArgs e)
{
string json1 = "{'one':'1','two':'2','three':'3'}";
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "scr", "Javascript:GetDateTime(" + json1 + ");", true);
}
[WebMethod]
public static string GetServerDateTime(string one, string two, string three)
{
return DateTime.Now.ToString() + " " + one + two + three;
}
据我所知
这篇关于如何使ajax功能起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文