Problems With XMLHTTP Request.

P

pbd22

Hi.

I am having a problem with an XMLHTTP request. The request itself
works just fine. But, if it evaluates to false I need to return the
error by adding to the errMsg txt as such:

errMsg += "<li>The code you entered was wrong.</li>";

This happens inside the http_request.onreadystatechange function.

The problem is, even when the request evaluates to false and the error
is added to the errMsg string, the message never actually appears in
the results. Every other error message does, just not the one
generated by the XMLHTTP request.

Any idea why this is happening?

Thanks.


if (strng15 == "") {
errMsg += "<li>You need to enter a code.</
li>";
} else {

try {

var http_request = false;

url = "data_code.aspx?code=" +
strng14;

if (window.XMLHttpRequest) { //
Mozilla, Safari, ...
http_request = new
XMLHttpRequest();
if
(http_request.overrideMimeType) {

http_request.overrideMimeType('text/xml');
}
}
else if (window.ActiveXObject) { //
IE
try {
http_request = new
ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new
ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}

if (!http_request) {
alert('Giving up :( Cannot
create an XMLHTTP instance');

//return BOOL_RETURN_STATUS;
}

http_request.onreadystatechange =
function() {

alert(http_request.responseText);

var jsonObj =
http_request.responseText.parseJSON();

alert(jsonObj);

if (http_request.readyState == 4)
{


document.getElementById("loading").style.visibility = "hidden";

if (http_request.status ==
200)
{
var result =
jsonObj.match.result;
alert(result);
if (result === false) {
errMsg += "<li>The code
you entered was wrong.</li>";
}

}

}
};
http_request.open('POST', url, true);
http_request.setRequestHeader('Content-
Type', 'application/x-www-form-urlencoded; charset=UTF-8');
http_request.send(null);
with
(document.getElementById("loading")) {
style.visibility = "visible";
}

} catch(e){

[SNIP... ]
 
M

Martin Honnen

pbd22 said:
I am having a problem with an XMLHTTP request. The request itself
works just fine. But, if it evaluates to false I need to return the
error by adding to the errMsg txt as such:

errMsg += "<li>The code you entered was wrong.</li>";

This happens inside the http_request.onreadystatechange function.

The problem is, even when the request evaluates to false and the error
is added to the errMsg string, the message never actually appears in
the results. Every other error message does, just not the one
generated by the XMLHTTP request.

Any idea why this is happening?
http_request.onreadystatechange =
function() {

alert(http_request.responseText);

Can you show us the responseText that the server sends?
And note that you should not try to access responseText before the
readyState is 3 or 4.
var jsonObj =
http_request.responseText.parseJSON();

alert(jsonObj);

if (http_request.readyState == 4)
{


document.getElementById("loading").style.visibility = "hidden";

if (http_request.status ==
200)
{
var result =
jsonObj.match.result;
alert(result);
if (result === false) {
errMsg += "<li>The code
you entered was wrong.</li>";

Where do you try to output the errMsg?
 
P

pbd22

Can you show us the responseText that the server sends?
And note that you should not try to access responseText before the
readyState is 3 or 4.






Where do you try to output the errMsg?

Thanks Martin.

This is client registration form validation.
The script is a bunch of IF statements that gather
errors in the errMsg string and output the results
in the FINALLY block.

The JSON coming back from the server looks like:

{"match": {"result": false}}

This tells me that the server tried to match the
user-typed code with the code in the image and the
result was a mismatch, or false.

And the errMsg string is output from the FINALLY
block as such:

[SNIP (xmlhttp code is prior to here)]

if (strng12 == "" || strng11 === "yyyy") {
errMsg += "<li>Please enter your birth
year.</li>";
}
if (strng13 == "") {
errMsg += "<li>Please enter your zip
code.</li>";
}

} // end outter try

catch(e){
alert(e.message);
} // end catch

finally {

if (errMsg == "")
{
BOOL_RETURN_STATUS = true;
} else {
errMsg += "<a href='#errMSG'></a>";
document.getElementById('errMSG').innerHTML =
errMsg;

document.getElementById('errTABLE').style.display = "";
window.location.hash="#errTABLE";
}

return BOOL_RETURN_STATUS;

}
 
M

Martin Honnen

pbd22 said:
The script is a bunch of IF statements that gather
errors in the errMsg string and output the results
in the FINALLY block.

The JSON coming back from the server looks like:

{"match": {"result": false}}

That looks fine for the way you use it.

And the errMsg string is output from the FINALLY
block as such:
finally {

if (errMsg == "")
{
BOOL_RETURN_STATUS = true;
} else {
errMsg += "<a href='#errMSG'></a>";
document.getElementById('errMSG').innerHTML =
errMsg;

I think you misunderstand how the code executes, the onreadystatechange
handler is a function of its own that is called after the send method
has been called. So using e.g.
try {
var http_request = false;
...
http_request.onreadystatechange = function () {
...
errMsg += "<li>The code you entered was wrong.</li>";
}
...
http_request.send(null);
catch (e) {
...
}
finally {
...
document.getElementById('errMSG').innerHTML = errMsg;
...
}
will never display the error message added in the onreadystatechange
handler as the finally block executes first, then later the
onreadystatechange handler is called.

You will need to make sure in the onreadystatechange handler that any
messages are output separately, the try/catch/finally does not help.

That's what I gather from your snippets, hope I understood them correctly.
 
P

pbd22

That looks fine for the way you use it.



I think you misunderstand how the code executes, the onreadystatechange
handler is a function of its own that is called after the send method
has been called. So using e.g.
try {
var http_request = false;
...
http_request.onreadystatechange = function () {
...
errMsg += "<li>The code you entered was wrong.</li>";
}
...
http_request.send(null);
catch (e) {
...
}
finally {
...
document.getElementById('errMSG').innerHTML = errMsg;
...
}
will never display the error message added in the onreadystatechange
handler as the finally block executes first, then later the
onreadystatechange handler is called.

You will need to make sure in the onreadystatechange handler that any
messages are output separately, the try/catch/finally does not help.

That's what I gather from your snippets, hope I understood them correctly.

OK, Thanks Martin.

So, if the verification code check cannot happen at the same
time as the other elements on the form, then it has to be a
different client-side output altogether. I assume that is what
you are saying? I think, maybe, my best bet would be to do the
captcha code validation using javascript's onblur event. This
frees up the ajax request from the try/catch/finally block.

If you don't agree let me know.
Otherwise, thanks for your help.

Regards,
Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top