Responsetext returns entire html page

Status
Not open for further replies.

fonzi

Member
You've probably come across this problem before. I have an Ajax site that has a response.write call in a c# page. This response is read on a javascript function as XMLHTTP.responseText.
It is returning what I want but there is an entire page of html after it. I have other places on the site where XMLHTTP.responseText is returned and there is no problem.
I think it is because previously on the button that calls the XMLHTTP.responseText there is another function that calls displays XMLHTTP.responseText.
I actually had this problem before and it fixed itself but I can't remember how.
I googled it and it said you can't get rid of it, you just have to parse out the part you want. Surely this can't be correct?
I could parse it but it's not reliable.
 

jason

New Member
Have a look at the server side script (ASP.net) and see what its sending back to the Ajax call and compare it to what its supposed to send back. To me it sounds like the asp script is generating an entire page when it should only be returning a short value or two which you can init some HTML element with.
 

fonzi

Member
The response that is being sent to the ajax call is in the page load event.
It runs through a load of if statements, loops in the page load event and then returns a string variable "ListOfIDs".
This is the C# code:

Response.ClearContent();
string ren2 = ListOfIDs;
Response.ContentType = "text/plain";
Response.Write(ren2);
Response.Flush();
Response.End();


when I put an alert in the JavaScript code like so:

alert(XMLHTTP.responseText);

the alert box displays the list of ids at the top but a load of html straight after it.

When I run it on my local pc it just returns what I want (the ListOfIDs) but on the live site it does the above.:mad:

If I can't get it to just return the variable, then is there a way I can edit the responsetext in C# to make it delete the html?
 

jason

New Member
If I can't get it to just return the variable, then is there a way I can edit the responsetext in C# to make it delete the html?
No no, never do that, you'd only end up writing redundant code and incurring potentially significant processing overheads which would have a knock on affect on performance/resource footprint/etc

It's interesting that you say it works on your local test environment but when you deploy it elsewhere you then get problems. It could be the configuration/type of environment you are deploying the ASP.NET code to and for some reason it's not executing it for you, just passing it through. I don't mean to assume you haven't this base covered, but are you sure the environment you are deploying your code to meets the correct runtime requirements?

From the info you've provided us with the only other thing I would suggest is watch how your ListOfIDs object is being initialized on the server before being sent back as a HTTPResponse to the Ajax client. From the code you posted, you just show us that you are sending it back, but you are not showing us how you are initialising it, but to be honest, from your observation that the code is working perfectly in one place and not in another would suggest to me that it's something outside of your application code that is causing you problems.

A few years back I used to do J2EE middleware component development, designing/coding and deploying EJB's, etc on jBoss, more often than not it was the configuration of the middleware container that used to cause me problems. Of course I still lost considerable amounts of time tearing the whole system apart before I finally came to this conclusion. Thankfully it became easier and quicker to troubleshoot over time.

Anyway best of luck. Post back if you're still stuck and I'll do my best to help. I'm not a .NET developer, but I am a software/web developer and will try to help where I can.
 

fonzi

Member
I think the problem is because in the aspx page that I am sending the Ajax request to already has processed an ajax request so there is a load of html when it is called a second time (because the page is already loaded)?


I call a testDates function from javascript code. This testDates function is contained in the back end (C#) code of an aspx page called "ActivateAjax.aspx"
The jscript code is as follows:
Code:
requestUrl="";
    //re-assigning AjaxPage variable to "ActivateAjax.aspx"
    AjaxPage = "ActivateAjax.aspx";
    //creating the URL to be sent by the XMLHTTP Request object 

    var requestUrl =AjaxPage  += "?Action=testDates&hotelstartdate=" + encodeURIComponent(startdate)+"&hotelenddate="+encodeURIComponent(enddate)+"&county="+encodeURIComponent(hotelCounty2)+"&price="+encodeURIComponent(rate)+"&room="+encodeURIComponent(roomchosen);

The C# code is as follows:

Code:
  public void testDates(DateTime startDate, DateTime endDate, string county, double price, string roomchosen)
       {
            string ren = null;
            ren = CompareDates(startDate, endDate).ToString();

            Response.ContentType = "text/plain";
            Response.Write(ren.ToString());
            

            if (ren == "False")
            {
//does a load of other code before this:

string ren2 = ListOfIDs;
                           
                            Response.Clear();
                            Response.ContentType = "text/plain";
                            string ren3 =Regex.Replace(ren2, "<.*?>", "");
                            Response.Write(ren3);
                            Response.Flush();
                            Response.End();

testDates calls CompareDates()
Code:
       public String CompareDates(DateTime startdate, DateTime enddate)

        {
            DateTime today = System.DateTime.Now;

            if (enddate < startdate)
            {

                return "startdateerror";

            }
            else if (startdate < today)
            {
                return "currentdateerror";
            }

            else if (startdate == enddate)
            {
                return "dateerror";
            }

            else
            {
                return "False";
            }

       

        }

Comparedates returns the "ren" variable which the jscript page receives and then gives the user a popup box depending on what it is.
However,the C#page also uses the "ren" variable in testDates() so I am calling Response.Write twice in the same function. Maybe this is problem?
 
Status
Not open for further replies.
Top