且听风吟

疯狂的IT菜鸟巢

博客园 首页 联系 订阅 管理

Ajax在不同浏览器中的兼容问题比较麻烦,我在最近的项目中遇到了这个问题。并且使用了一种奇怪的方法解决了它,现在拿出来大家讨论讨论:

下面是前端代码:

<script type="text/javascript">

var xmlHttp;

var as;

var state = 0;

function mouseover() 

{

   var id = "<%=cvE_Mail.ClientID%>";

 

    getajax();

   

     if (as == 0)

     {

                     alert("Your WebBrowser do not support our service,Choose IE7 or FireFox and try again!");

     }

    else

    {

         //在回传的时候邦定事件也不同

         if (as == 1)

            {

                 xmlHttp.onreadystatechange = myResponseHandler;

            }       

            else

            {

                    xmlHttp.onload = myResponseHandler;

                       xmlHttp.onerror = myResponseHandler;

             }

            //在这里open的时候如果方式选为get,很奇怪firefox的处理端收不到传过去的参数

           //在这里可以看到最后一个参数也是不同的,如果不这样设置那么返回的responseText 很有可能是空的

            if (as == 1)

            {

                    xmlHttp.open("POST", "ajax.ashx", true);

             }

             else

              {

                     xmlHttp.open("POST", "ajax.ashx", false);

                }

               xmlHttp.send(document.getElementById(id).value);

         }

};

function getajax()

{

   //首先是创建的时候不同

    try {

              xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");

              as = 1;

           } catch (e) {

               try {

                            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");

                            as = 1;

                           } catch (e) {

                          try {

                                      xmlHttp = new XMLHttpRequest();

                                      as = 2;

 

                              } catch (e) {

                             xmlHttp = null;

                          as = 0;

                                   }

                           }

               }

}

 

function myResponseHandler()

{

             var id = "<%=Hidebutton.ClientID%>";

             if (xmlHttp.readyState == 4)

             {

                      document.getElementById(id).value=xmlHttp.responseText;

             }

};

 

</script>

后端代码(asp.net):

public class CAjax : IHttpHandler

 

{

             public void ProcessRequest(HttpContext context)

              {

                      //首先读前端传过来的值

 

                      HttpRequest request = context.Request;

                      System.IO.Stream s = request.InputStream;

                      int count = 0;

                       byte[] buffer = new byte[1024];

                       StringBuilder builder = new StringBuilder();

                       while ((count = s.Read(buffer, 0, 1024)) > 0)

                       {

                                   builder.Append(Encoding.UTF8.GetString(buffer, 0, count));

                         }

                        string request= builder.ToString();//request 就是传过来的参数

                         ..................

                        //result 为想要返回前端的结果

                         context.Response.Write(result);

                   }

}

    这个方案我在firefox,ie,遨游里面都测试过了没有问题,如果有感兴趣的可以给我发信 :zhaotiantang520@live.cn

 

posted on 2008-10-31 17:51  微风吟  阅读(790)  评论(0编辑  收藏  举报