3

我需要从我的服务器端代码中获取输出,但我能够获取它。我在母版页中使用了脚本管理器和脚本管理器代理控制。如何获取从 cs 文件返回的字符串这是我的 Default.aspx 代码

<asp:UpdateProgress runat="server">
<ProgressTemplate >
 Loading..........
 </ProgressTemplate>
 </asp:UpdateProgress>
  <asp:UpdatePanel runat="server">
  <ContentTemplate>
  <asp:Label ID="lbl" Text="Enter Your Name" runat="server"></asp:Label>
 <input type="text" id="txt" /><br />
  <input type="button" id="btn" value="Submit" onclick="makeGetRequest()" />  
  <div id="description">Hello</div>
 </ContentTemplate>   
 </asp:UpdatePanel>

  <script language="Javascript" type="text/javascript" >
   function createRequestObject() {
    var tmpXmlHttpObject;

    if (window.XMLHttpRequest) {
        // Mozilla, Safari would use this method ...
        tmpXmlHttpObject = new XMLHttpRequest();

    } else if (window.ActiveXObject) {
        // IE would use this method ...
        tmpXmlHttpObject = new ActiveXObject("Microsoft.XMLHTTP");
    }

    return tmpXmlHttpObject;
     }


    var http = createRequestObject();

     function makeGetRequest() {


    http.open('post', 'Default.aspx/greet',true);

    //assign a handler for the response
    http.onreadystatechange = processResponse;

    //actually send the request to the server
    http.send(null);
    }


    function processResponse() {


    if (http.readyState == 4) {


        var response = http.responseText.toString();

        alert("Inside here before");

        document.getElementById('description').innerHTML = response;
        alert("Inside here after");


    }
  }


</script>




 // my default.aspx.cs file


[WebMethod(EnableSession=false)]
 public static string greet()
 {
    return "hello";
 } 

我正在复制页面的所有内容,而不是从中获取响应

4

1 回答 1

1

该行var http = createRequestObject();必须是function makeGetRequest()函数内部的第一行。无论如何,我正在发布一个工作代码。

<script language="Javascript" type="text/javascript" >
function makeGetRequest() {
    var http;
    if (window.XMLHttpRequest) {
        http = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        http = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4) {
            var response = http.responseText.toString();
            alert("Inside here before");
            document.getElementById('description').innerHTML = response;
            alert("Inside here after");
        }
    }

    http.open('post', 'Default.aspx/greet', true);
    http.send(null);
}
</script>
于 2013-07-28T09:56:50.313 回答