The responseText property of XMLHttpRequest object is returning the whole H - TechRepublic
Question
January 22, 2008 at 04:58 AM
packer.lean

The responseText property of XMLHttpRequest object is returning the whole H

by packer.lean . Updated 18 years, 6 months ago

Hello all,

I am a newbie to the AJAX programming techniques. I want to update my database as per choice made by the user and inform him/her about the same if the updation has been done successfully. The platform is ASP.NET

Following is my implementation:

The file containing following two javascript functions has been included in my ASPX file.

This function (i.e. submitValue) is called on the button click and the parameter (i.e. value) is the data I want to update on my database. The function “getXmlHttp()” in bold is returning the required XMLHttpRequest object by using the suitable method as per the browser detected. As I want to inform the user on the same page so the variable “requestURL” passed in the open() function contains the URL of the same page with updated querystring.

function submitValue(value)
{
try
{
//Create the XMLHttpRequest object.
xmlHttp = getXmlHttp();

//Call the function to process the response from the server
xmlHttp.onreadystatechange = stateChanged;

//Send request to the server
var requestURL = location.href+”&q=”+value;

xmlHttp.open(‘GET’,requestURL,true);
xmlHttp.send(null);

}
catch(e)
{
alert(“Error : Sending request”);
}
}

Following javascript function which is called above is used to process the server response.

function stateChanged()
{
try
{
if (xmlHttp.readyState==4 ||xmlHttp.readyState==’complete’)
{
var retValue;
retValue = xmlHttp.responseText;

if(retValue==”0″)
document.getElementById(“divRateInfo”).innerHTML = “Rating submition failed. Try again.”;

else

document.getElementById(“divRateInfo”).innerHTML = “Rating successfully submited!”;

}
}
catch(e)
{
alert(“Error : Processing response”);
}
}

Finally following is the code behind written on the corresponding ASPX.CS file. The method is called on the page load.

private void SaveRating()
{
try
{
if (Request.QueryString[“q”] != null)
{
int nUserRate = Convert.ToInt32(Request.QueryString[“q”].ToString());

SqlParameter[] sqlParam = new SqlParameter[2];
sqlParam[0] = new SqlParameter(“@rate”, SqlDbType.Int);
sqlParam[0].Value = nUserRate;
sqlParam[1] = new SqlParameter(“@id”, SqlDbType.Int);
sqlParam[1].Value = Convert.ToInt32(this.Request.QueryString[“id”].ToString());
object strInfo = SqlHelper.ExecuteScalar(ConfigurationSettings.AppSettings[“ConnectionString”], CommandType.StoredProcedure, “sp_UpdateRating”, sqlParam);

Response.Expires = -1;
Response.Write(strInfo.ToString());
}
}
catch (Exception objExcep)
{
Response.Expires = -1;
Response.Write(“0”);
}
}

Everything works as per expectation, even my database is getting updated perfectly but the “responseText” property in the function “stateChanged()” above is returning the code for my ASPX page instead of just 0 or 1 as expected.

O my god the post has become so long but I wanted to make myself clear. Can anyone suggest any solution? Please help!

This discussion is locked

All Comments