P
pbd22
All,
Need some help. My xmlhttp request isn't responding. I think it has to
do with the use of onsubmit as the calling function but, I am not
clear on what exactly is going on here. Should i be setting "return =
true" for the xmlhttp request to work? I can verify that the server
code gets called and I can step through the code in debug. But the
responseText does not alert. It seems that the response handler never
gets initiated - no alert, no nothing. I am under deadline and could
really use some help. Thanks!
So, this is the call from the onsubmit of the form tag:
<form id="form1" name="form1" onsubmit="return clientvalidation();"
method="post" action="create.aspx" enctype="multipart/form-data">
This is the script distilled:
<script type="text/javascript">
function clientvalidation(){
genericXMLHTTP("GET", "ajaxtest.aspx", "", collisionResponseHandler,
true);
}
function collisionResponseHandler(response)
{
alert(response.responseText);
}
function genericXMLHTTP(method, url, parameters, responseHandler,
asynchronous)
{
if (typeof method != 'string')
{
alert("Invalid request method.");
return false;
}
method = method.toUpperCase();
if (method != 'POST' && method != 'GET')
{
alert("Invalid request method.");
return false;
}
if (typeof parameters != 'string'){ parameters = '';}
if (typeof asynchronous == 'undefined'){ asynchronous = true;}
// build the request
var ajaxRequest = null;
if (window.XMLHttpRequest){ ajaxRequest = new
XMLHttpRequest();}
else if (window.ActiveXObject){ ajaxRequest = new
ActiveXObject("Microsoft.XMLHTTP");}
// bind the callback.
function ajaxBindCallback()
{
if (ajaxRequest.readyState == 4)
{
if (ajaxRequest.status == 200)
{
if (responseHandler)
{ responseHandler(ajaxRequest);}
}
else
{
if (ajaxRequest.status == 404)
{
alert("The requested service could not be
found.");
}
else
{
alert("There was a problem retrieving the xml
data:\n" +
ajaxRequest.status + ":\t" +
ajaxRequest.statusText + "\n" +
ajaxRequest.responseText);
}
}
}
}
// send the request
if (ajaxRequest)
{
if (asynchronous){ ajaxRequest.onreadystatechange =
ajaxBindCallback;}
if (method == "GET")
{
url = url + '?' + parameters;
parameters = null;
}
ajaxRequest.open(method, url, asynchronous);
if (method == 'POST')
{
ajaxRequest.setRequestHeader('Content-Type',
'application/x-www-form-urlencoded; charset=UTF-8');
}
ajaxRequest.send(parameters);
if (!asynchronous){ ajaxBindCallback();}
}
else
{
alert("Unable to build XMLHttpRequest");
}
}
</script>
And this is my server code (for testing):
Partial Class ajaxtest
Inherits System.Web.UI.Page
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Response.Write("THIS WORKS")
End Sub
End Class
Need some help. My xmlhttp request isn't responding. I think it has to
do with the use of onsubmit as the calling function but, I am not
clear on what exactly is going on here. Should i be setting "return =
true" for the xmlhttp request to work? I can verify that the server
code gets called and I can step through the code in debug. But the
responseText does not alert. It seems that the response handler never
gets initiated - no alert, no nothing. I am under deadline and could
really use some help. Thanks!
So, this is the call from the onsubmit of the form tag:
<form id="form1" name="form1" onsubmit="return clientvalidation();"
method="post" action="create.aspx" enctype="multipart/form-data">
This is the script distilled:
<script type="text/javascript">
function clientvalidation(){
genericXMLHTTP("GET", "ajaxtest.aspx", "", collisionResponseHandler,
true);
}
function collisionResponseHandler(response)
{
alert(response.responseText);
}
function genericXMLHTTP(method, url, parameters, responseHandler,
asynchronous)
{
if (typeof method != 'string')
{
alert("Invalid request method.");
return false;
}
method = method.toUpperCase();
if (method != 'POST' && method != 'GET')
{
alert("Invalid request method.");
return false;
}
if (typeof parameters != 'string'){ parameters = '';}
if (typeof asynchronous == 'undefined'){ asynchronous = true;}
// build the request
var ajaxRequest = null;
if (window.XMLHttpRequest){ ajaxRequest = new
XMLHttpRequest();}
else if (window.ActiveXObject){ ajaxRequest = new
ActiveXObject("Microsoft.XMLHTTP");}
// bind the callback.
function ajaxBindCallback()
{
if (ajaxRequest.readyState == 4)
{
if (ajaxRequest.status == 200)
{
if (responseHandler)
{ responseHandler(ajaxRequest);}
}
else
{
if (ajaxRequest.status == 404)
{
alert("The requested service could not be
found.");
}
else
{
alert("There was a problem retrieving the xml
data:\n" +
ajaxRequest.status + ":\t" +
ajaxRequest.statusText + "\n" +
ajaxRequest.responseText);
}
}
}
}
// send the request
if (ajaxRequest)
{
if (asynchronous){ ajaxRequest.onreadystatechange =
ajaxBindCallback;}
if (method == "GET")
{
url = url + '?' + parameters;
parameters = null;
}
ajaxRequest.open(method, url, asynchronous);
if (method == 'POST')
{
ajaxRequest.setRequestHeader('Content-Type',
'application/x-www-form-urlencoded; charset=UTF-8');
}
ajaxRequest.send(parameters);
if (!asynchronous){ ajaxBindCallback();}
}
else
{
alert("Unable to build XMLHttpRequest");
}
}
</script>
And this is my server code (for testing):
Partial Class ajaxtest
Inherits System.Web.UI.Page
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Response.Write("THIS WORKS")
End Sub
End Class