jquery的get和post提交

时间:2022-12-03 20:16:44

前台代码:

<body>
<form id="myForm" runat="server">
Name:
<input type="text" name="name" id="txtName" />
Comment:
<textarea name="comment" rows="2" id="txtComment"></textarea>
<input id="btnPost" type="button" value="Post Submit" />
<input id="btnGet" type="button" value="Get Submit" />
</form>
</body>


<script type="text/javascript">
$(document).ready(

////post方式
function () {
$("#btnPost").click(
function () {
$.post(
"MyDemoHandler.ashx",
$("#myForm").serialize(), //提交表单,相当于CheckCorpID.ashx?ID=XXX
function (msg) { alert(msg); } //操作成功后的操作!msg是后台传过来的值
);
});


 

////GET方式
$("#btnGet").click(
function () {
$.get(
"MyDemoHandler.ashx",
{
"comment": $.trim($("#txtComment").val()),
"name": $.trim($("#txtName").val())
}, //提交表单,相当于CheckCorpID.ashx?ID=XXX
function (msg) { alert(msg); } //操作成功后的操作!msg是后台传过来的值
);
});

}
);
</script>


 


还有:

 

$.ajax({
type: "POST",
url: "http://localhost:3071/Handler1.ashx",
data: "data={\"ClassId\":0,\"Id\":2,\"Name\":\"aaa\"}",
success: function (msg) {
alert(msg);
},
});


 

后台代码:

 

<%@ WebHandler Language="C#" Class="MyDemoHandler" %>
using System;
using System.Web;
using System.Text;
public class MyDemoHandler : IHttpHandler {

public void ProcessRequest (HttpContext context) {

StringBuilder recievdStr = new StringBuilder(100);
recievdStr.AppendLine("name=" + context.Request["name"]);
recievdStr.AppendLine("comment=" + context.Request["comment"]);


context.Response.ContentType = "text/plain";
context.Response.Write("Hello World./n/r" + recievdStr.ToString());
}

public bool IsReusable {
get {
return false;
}
}
}