3

I need to get output from my server side code but i am able to fetch it. I have used script manager and script manager proxy control in master page. How can i get the string that is being returned from the cs file Here is my Default.aspx code

<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";
 } 

I am getting all contents of the page copied instead of getting response from it

Ashish Babu
  • 1,167
  • 9
  • 20

1 Answers1

1

the line var http = createRequestObject(); have to be the first line inside function makeGetRequest() function. anyway, i am posting a working code.

<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>
YaakovHatam
  • 2,314
  • 2
  • 22
  • 40