locked
context.Response.Write is not working on mozilla,safari ... RRS feed

  • Question

  • User863311691 posted

    Hi All,

    I m using Asp.net 2.0 to develop my web app.
    i m using Webhandler (.ashx) pages to wrrite some html from backend..
    the code is work finr for IE but it is not work for mozilla,safari..
    here is the code..
    In Javascript i write:-


    var xmlhttp = null;

    if (window.XMLHttpRequest) { // if Mozilla, Safari etc
    xmlhttp = new XMLHttpRequest();

    }
    else if (window.ActiveXObject) { // if IE
    try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");

    } catch (e) {
    try {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

    } catch (e) {}
    }
    }

    if (xmlhttp)
    {
    xmlhttp.open("POST", "./htask_getDevicesGroupWise.ashx", true); //varAsynx = true
    xmlhttp.onreadystatechange = function()
    {
    if (xmlhttp.readystate ==4) //4 is a success
    {
    try{
    var result = xmlhttp.responseText;
    eval(result);
    LoadingHide();
    }
    catch(e)
    {
    //alert(e.message)
    //throwError(e.message,'fn_runHandler','Common Handler','ve.js');
    }
    }
    }
    xmlhttp.send(null);
    }


    And in Handler page i just write

     Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
    context.Response.Write("alert('sfs');")
    End Sub


    alert message is not showing on mozilla,safari but it is working in IE.
    so i think context.Response.Write is not working ..
    Why? D'Oh!

    Please help!!!
    Thanks in advance
    Nitin

     

    Friday, January 23, 2009 9:06 AM

Answers

  • User653228039 posted

    I can't get your RequestObject to work.  I'm not sure it is set up right.  Have you used this object in other successful applications before?

     

    See if you can get better luck with this object:

     

    function XML_HTTP_class()
    {
    	var ns=!document.all;
    	var ax=null;
    	do_init();
    
    	function do_init()
    	{
    		function err(e)
    		{
    			program_abort("foundation_download_class: Cannot create XMLHTTP: ",e);
    		}
    		if(ns)
    		{
    			try
    			{
    				ax=new XMLHttpRequest()
    			}
    			catch(e)
    			{
    				err(e)
    			}
    		}
    		else if (window.ActiveXObject)
    		{
    			try
    			{
    				ax=new ActiveXObject("Microsoft.XMLHTTP");
    			}
    			catch(e)
    			{
    				err(e)
    			}
    		}
    		else
    		{
    			program_abort("Your browser does not support XMLHTTP");
    		}
    	}
    	
    	this.getResponseText=function()
    	{
    		return ax.responseText;
    	}
    	
    	this.getResponse=function()
    	{
    		return ax.responseText;
    	}
    	
    	this.GET=function(url)
    	{
    		ax.open("get",url,false);
    		if(ns)
    			ax.send(null);
    		else
    			ax.send();
    	}
    	
    	this.postForm=function(url,sss)
    	{
    		ax.open("post",url,false);
    		ax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    		ax.send(sss);
    	}
    	
    	this.postXML=function(url,sss)
    	{
    		ax.open("post",url,false);
    		ax.setRequestHeader("Content-Type","text/xml");
    		ax.send(sss);
    	}
    }

     

      

    var httpRequest=new XML_HTTP_class();
    
    function getAjaxInfo(url)
    {
    	httpRequest.GET(url);
    	return httpRequest.getResponseText();
    }
    
     

     

    Then, when you need to do an AJAX call, you simply do this:

    var requestStr = "somestuff.aspx";
    var responseStream=getAjaxInfo(requestStr);
    var data=eval("("+responseStream+")");
    

     

    I've had a high degree of success with this.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 23, 2009 1:15 PM

All replies

  • User653228039 posted

    Response.Write is happening on the server before it is ever sent to the client, so it is independent of the browser.  Perhaps there is a problem with how your HttpRequest object is set up.  Can you verify that this setup works with other examples?  I'm guessing that it is entering the "catch" block for mozilla and safari, but your lines are commented out, so there is no javascript to execute in their examples.  I'll take a look at your HttpRequest object.

    Friday, January 23, 2009 11:50 AM
  • User1407569999 posted

     Why don't you try alert(result) instead of eval(result) so you can see what is being returned.  It might be as simple as the content-type or encoding-type -- that's my guess actually.  I spent a while once on a similar problem relating to traversing XML returned from a webservice and it turned out to be an issue with setting (or not in my case) the Content-Type in the response.

    Friday, January 23, 2009 12:04 PM
  • User653228039 posted

    That's actually not a bad suggestion, however, the content-type only seems to matter when you are expecting XML data.  I use JSON as my return data format, and I don't need to set the content type.

     

    Give it a try though:

    Response.ContentType = "text/xml";

    put that before your Response.Write.

     

    Friday, January 23, 2009 12:58 PM
  • User653228039 posted

    I can't get your RequestObject to work.  I'm not sure it is set up right.  Have you used this object in other successful applications before?

     

    See if you can get better luck with this object:

     

    function XML_HTTP_class()
    {
    	var ns=!document.all;
    	var ax=null;
    	do_init();
    
    	function do_init()
    	{
    		function err(e)
    		{
    			program_abort("foundation_download_class: Cannot create XMLHTTP: ",e);
    		}
    		if(ns)
    		{
    			try
    			{
    				ax=new XMLHttpRequest()
    			}
    			catch(e)
    			{
    				err(e)
    			}
    		}
    		else if (window.ActiveXObject)
    		{
    			try
    			{
    				ax=new ActiveXObject("Microsoft.XMLHTTP");
    			}
    			catch(e)
    			{
    				err(e)
    			}
    		}
    		else
    		{
    			program_abort("Your browser does not support XMLHTTP");
    		}
    	}
    	
    	this.getResponseText=function()
    	{
    		return ax.responseText;
    	}
    	
    	this.getResponse=function()
    	{
    		return ax.responseText;
    	}
    	
    	this.GET=function(url)
    	{
    		ax.open("get",url,false);
    		if(ns)
    			ax.send(null);
    		else
    			ax.send();
    	}
    	
    	this.postForm=function(url,sss)
    	{
    		ax.open("post",url,false);
    		ax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    		ax.send(sss);
    	}
    	
    	this.postXML=function(url,sss)
    	{
    		ax.open("post",url,false);
    		ax.setRequestHeader("Content-Type","text/xml");
    		ax.send(sss);
    	}
    }

     

      

    var httpRequest=new XML_HTTP_class();
    
    function getAjaxInfo(url)
    {
    	httpRequest.GET(url);
    	return httpRequest.getResponseText();
    }
    
     

     

    Then, when you need to do an AJAX call, you simply do this:

    var requestStr = "somestuff.aspx";
    var responseStream=getAjaxInfo(requestStr);
    var data=eval("("+responseStream+")");
    

     

    I've had a high degree of success with this.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 23, 2009 1:15 PM