jquery ajax在aspx的调用

     前台代码

 

 1 <head runat="server">
 2     <title></title>
 3     <script src="../../Scripts/jquery-1.8.3.js" type="text/javascript"></script>
 4     <script type="text/javascript">
 5         $(document).ready(function () {
 6             $.ajax({
 7                 // url: "test_2.aspx",
 8                 url:"test_3.ashx",
 9                 data: { "Id": "3" },
10                 type: "post",
11                 dataType: "text",
12                 beforeSend: function () {
13                     alert("开始");
14                 },
15                 cache: true,
16                 success: function (str) {
17                     alert(str);
18                 },
19                 error: function (msg) {
20                     alert("错误");
21                 }
22             });
23         });
24     </script>
25 </head>
View Code

    

     后台代码 aspx

 

 1 protected void Page_Load(object sender, EventArgs e)
 2         {
 3             string str = "false";
 4             if (!IsPostBack)
 5             {
 6                 string id = Request["Id"] != null ? Request["Id"].ToString() : "";
 7                 if (id != "")
 8                 {
 9                     str = "success";
10                 }
11                 else
12                 {
13                     str = "error";
14                 }
15             }
16             Response.Write(str);
17             Response.End();
18         }
View Code

 

    后台代码 ashx

 

 1  public void ProcessRequest(HttpContext context)
 2         {
 3             string str = "false";
 4             string id = context.Request["Id"] != null ? context.Request["Id"].ToString() : "";
 5             if (id != "")
 6             {
 7                 str = "success";
 8             }
 9             else
10             {
11                 str = "error";
12             }
13             context.Response.ContentType = "text/plain";
14             context.Response.Write(str);
15         }
16 
17         public bool IsReusable
18         {
19             get
20             {
21                 return true;
22             }
23         }
View Code

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。