weixin_33728268 2012-09-06 18:04 采纳率: 0%
浏览 105

使用ASP.NET处理xhr

I would like to send a POST asynchronously from client side (JavaScript) to server side (ASP.Net) with 2 parameters: numeric and long formated string.

I understand the long formated string must have encodeURIComponent() on it befor passing it.

My trouble is I want to embed the long encoded string in body request and later open it from C# on server side.

Please, can you help me? I'm messing too much with ajax, xhr, Request.QueryString[], Request.Form[], ....

  • 写回答

1条回答 默认 最新

  • weixin_33694172 2012-09-06 18:20
    关注

    First, create an 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; }
        }
    }
    

    Then register it:

    <configuration>
        <system.web>
            <httpHandlers>
                <add verb="*" path="*.ashx" 
                      type="HelloWorldHandler"/>
            </httpHandlers>
        </system.web>
    </configuration>
    

    Now call it - using jQuery Ajax:

    $.ajax({
          type : "POST",
          url : "HelloWorldHandler.ashx",
          data : {id: "1" , LongString: "Say Hello"},
          success : function(data){
                 //handle success
          }
     });
    

    NOTE

    Totally untested code but it should be very close to what you need.

    I just tested and it works out of the box. This is how I called it:

    <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" />
    
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧