Kirk wrote:
>
OK, I am once again banging my head against the wall. Here is what I
have so far.
I created a page that uses the passed parameter to determine what to
put in the response. This looks like:
'Get the passed parameter from the page
If Not (Request.Params("sPartNumber") Is Nothing) Or
(Request.Params("sPartNumber") = "") Then
This isn't directly related to your JS question (and I haven't
programmed in VB for over a decade, so I could be wrong), but it seems
to me that this ought to be:
If Not(Request.Params("sPartNumber") Is Nothing Or
Request.Params("sPartNumber") = "") Then
or
If Not(Request.Params("sPartNumber") Is Nothing) And
Not(Request.Params("sPartNumber") = "") Then
Otherwise your logic will always enter the If-block when
Request.Params("sPartNumber") = "" (which is probably the opposite of
what you want).
<snip>
This works, in the sense that if I open the page like
this :"PartExistCheck.aspx?sPartNumber=230-001", it outputs True or
False based on the value passed.
Your code did just as you promised, but I am still mucking something
up. When the validation runs, I can set a breakpoint in the VB code
and see that it is executing (yay!). However, the value is not being
passed - it always comes back as [nothing]. Am I looking at the wrong
parameter?
function CheckPart(sender, args)
{
var async = new XMLHttpRequest();
async.open("POST", 'PartExistCheck.aspx', true);
async.setRequestHeader('Content-Type',
'application-x-www-form-urlencoded');
async.onreadystatechange = function()
{
if(async.readyState == 4)
{
if(async.responseText == 'true')
{
args.IsValid = false;
return;
}
else
{
args.IsValid = true;
}
}
}
async.send('sPartNumber=' + escape('230-001'));
}
<snip>
The thing you are missing is the principle of asynchronous operation.
By the time the response comes back and the anonymous function() is
executed, you are no longer in the CheckPart function, in terms of
execution flow. That function has already returned (nothing, in this
case) and the program has moved on.
Here is the sequence of events in this case:
1. CheckPart is called.
2. CheckPart sends off HTTP request.
3. CheckPart returns nothing. "args" reference parameter is untouched
at this point.
4. Whatever function called CheckPart sees that the call did nothing.
It becomes distraught and goes off to the pub.
5. An unspecified amount of time passes - probably a few hundred
miliseconds.
6. Anonymous function() is called. outer if-block is entered.
7. args.IsValid is set to either true or false.
8. Anonymous function() returns nothing. "args" is now altered in the
desired sense, but the function that called CheckPart to begin with is
already off at the pub having a drink and can't do anything about it.
Unfortunately, you have just touched upon one of the most confusing
aspects of AJAX (asynchronous operation) and one of the most confusing
aspects of the javascript language (scoping) at the same time!
The key point here is that you simply must not look at the inner
function() as something that will be executed immediately*. It will be
executed (hopefully) at some point in the future, so instead of
monkeying with local data, which will be passed back to another function
that does some voodoo, it should use the information it has to do the
voodoo itself. I don't know what your code does after CheckPart
returns, so I can't really help you with that part. But chances are you
will need a pretty major restructuring of your javascript, consisting
of taking everything that CheckPart's caller was supposed to do after
CheckPart returned, and either doing it in your anonymous function(), or
putting it in a separate function that gets called by the anonymous
function.
If you need an example of converting a synchronous call to an
asynchronous one, I'll post one (but first you should take a crack at it
and see what you can do).
Jeremy
*It is actually *possible* to make your request operate in a synchronous
fashion, which would make your code here work. HOWEVER, this is
generally accepted to be a Real Bad Idea and a cardinal programming sin.
I'll tell you how to do it, but you shouldn't. Replace the last
parameter to async.open - change true to false. Now your request will
block until it is complete. The problem is, this could potentially be
forever (if the server dies, for example) and it will freeze up the browser.