0

i am using AjaxRequest.Get() method from AjaxRequest.
following is the inline javascript in analysis.aspx

 function getAnalysis(type) {            
        var innerHtml;      
        AjaxRequest.get(
            {
                'url': 'getAnalysis.aspx?type=' + type
                , 'onSuccess': function (req) { innerHtml = req.responseText; }
            }
        );
        document.getElementById("div_analysis").innerHTML = innerHtml;
    }

when getAnalysis(type) is called in analysis.aspx everything goes fine - ajax request is properly submitted and response is send properly. But at the end value of innerHTML remains undefined.

Following is the code of getAnalysis.aspx -

 protected void Page_Load(object sender, EventArgs e)
 {
    if(type == "somwthing") str = load();    
    Response.Clear();     
    Response.CacheControl = "no-cache";              
    Response.Write(str);      
    Response.End();     
 }

When i debugged javascript using google chrome, i found that value of innerHMTL is undefined, although everything went fine.
So i dont understand why AjaxRequest class is not accepting text output from Reponse.Write().

P.S. : I have also tried Response.ContentType = "text/Html"; and Reponse.Fluch().
please guide me thnx in advance.

j4m4l
  • 328
  • 2
  • 10
  • 22

1 Answers1

0

You need to set the div contents in the onsuccess function since it is called asynchronously when the AJAX request completes

function getAnalysis(type) {             
        var innerHtml;       
        AjaxRequest.get( 
            { 
                'url': 'getAnalysis.aspx?type=' + type 
                , 'onSuccess': function (req) { document.getElementById("div_analysis").innerHTML = req.responseText; } 
            } 
        ); 
    } 
Stilgar
  • 22,354
  • 14
  • 64
  • 101
  • thxn now its working, but there is one more thing, the returned html contains some script tag which are not evaluated ( i guess bcoz of asynchronous call) so how do i get them evaluated. – j4m4l May 19 '12 at 11:17
  • 1
    You can do it by extracting the script elements (lets say with jQuery) and then doing an eval on them. The best you can do is not include script elements in the HTML you transmit. In most cases there is no reason to do this. Also I believe the problem occurs only when you try to add a script that needs to be executed right away. If you add a script that just declares some function everything should work. And finally if you feel like you really need to do that then ask a separate question since it is a separate problem. Make sure tochoose the title wisely and have an example with no server code – Stilgar May 20 '12 at 00:19