我想使用 2 个参数从客户端(JavaScript)异步发送 POST 到服务器端(ASP.Net):数字和长格式字符串。
我知道长格式的字符串在传递之前必须有 encodeURIComponent() 。
我的麻烦是我想在正文请求中嵌入长编码字符串,然后在服务器端从 C# 打开它。
拜托,你能帮帮我吗?我对 ajax、xhr、Request.QueryString[]、Request.Form[]、...
我想使用 2 个参数从客户端(JavaScript)异步发送 POST 到服务器端(ASP.Net):数字和长格式字符串。
我知道长格式的字符串在传递之前必须有 encodeURIComponent() 。
我的麻烦是我想在正文请求中嵌入长编码字符串,然后在服务器端从 C# 打开它。
拜托,你能帮帮我吗?我对 ajax、xhr、Request.QueryString[]、Request.Form[]、...
首先,创建一个 HTTPHandler:
using System.Web;
public class HelloWorldHandler : IHttpHandler
{
public HelloWorldHandler()
{
}
public void ProcessRequest(HttpContext context)
{
HttpRequest Request = context.Request;
HttpResponse Response = context.Response;
//access the post params here as so:
string id= Request.Params["ID"];
string longString = Request.Params["LongString"];
}
public bool IsReusable
{
// To enable pooling, return true here.
// This keeps the handler in memory.
get { return false; }
}
}
然后注册它:
<configuration>
<system.web>
<httpHandlers>
<add verb="*" path="*.ashx"
type="HelloWorldHandler"/>
</httpHandlers>
</system.web>
</configuration>
现在调用它 - 使用 jQuery Ajax:
$.ajax({
type : "POST",
url : "HelloWorldHandler.ashx",
data : {id: "1" , LongString: "Say Hello"},
success : function(data){
//handle success
}
});
我刚刚测试过,它开箱即用。我是这样称呼它的:
<script language="javascript" type="text/javascript">
function ajax() {
$.ajax({
type: "POST",
url: "HelloWorldHandler.ashx",
data: { id: "1", LongString: "Say Hello" },
success: function (data) {
//handle success
}
});
}
</script>
<input type="button" id="da" onclick="ajax();" value="Click" />