AJAX:我无法从服务器端获取数据

时间:2013-07-27 09:58:19

标签: ajax jquery asp.net-ajax ajaxcontroltoolkit ajax4jsf

我需要从服务器端代码获取输出,但我可以获取它。 我在母版页中使用了脚本管理器和脚本管理器代理控制。 如何获取从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";
 } 

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

1 个答案:

答案 0 :(得分: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>