ASP.NET MVC2中三种ajax实现方式-原始JavaScript

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>


 Index


   

Comments

   
   
           
       

        <%= Html.TextArea("Comment", new{rows=5, cols=50}) %>
       
                                             


   

 function getXmlHttpRequest() {
            var xhr;
            //check for IE implementation(s)
            if (typeof ActiveXObject != 'undefined') {
                try {
                    xhr = new ActiveXObject("Msxml2.XMLHTTP");
                } catch (e) {
                    xhr = new ActiveXObject("Microsoft.XMLHTTP");
                }
            } else if (XMLHttpRequest) {
                //this works for Firefox, Safari, Opera 
                xhr = new XMLHttpRequest();
            } else {
                alert("对不起,你的浏览器不支持ajax");
            }

            return xhr;
        }
   
        function getMessage() {
            //get our xml http request object
            var xhr = getXmlHttpRequest();
 
            //prepare the request
            xhr.open("GET", "Comment/AddCommentServer?comment=" + document.getElementById("Comment").value, true)
           
            //setup the callback function
            xhr.onreadystatechange = function() {
             //readyState 4 means we're done
             if(xhr.readyState != 4) return;
     
             //populate the page with the result
             document.getElementById('comments').innerHTML = document.getElementById('comments').innerHTML + xhr.responseText;
            };
  
            //fire our request
            xhr.send(null);
        }

    

你可能感兴趣的:(ASP.NET MVC2中三种ajax实现方式-原始JavaScript)