...

sychronous Ajax call not completing

friscofrankie
06-08-2007, 02:26 PM
I have a drag&drop content manager tat supports multiple languages for eh same site. the site has multiple nested categories. As new languages are added to each of the page entries the user is required to add language support for all parents and grandparents. This part works fine.
the drag&drop allows the restructuring of the DB by moving sub-categories or "files" (really Table entries that PHP uses to build pages) to new parents. In order for a child to be move to the new category I need to ensure the user has added language support for each of the languages supported by the moved item.
I am attempting to catch the server-side generated Error message if there is one and send an alert with the responseText of the server script.


function verifyLang(dropId, itemId)
{
if(itemId == 'bro'){itemNo = dragItem.getAttribute('pg_id');}
else if(itemId == 'pg_'){itemNo = dragItem.getAttribute('pg_id');}

var queryString = 'http://'+location.host+'/admin/add-edit.php?verify_mv=category&param='+dropId+':'+itemNo;
verifyDropRequest.open("GET", queryString, false);

verifyDropRequest.onreadystatechange = function()
{
if((verifyDropRequest.readyState == 4)&&(verifyDropRequest.status == 200))
{
if(verifyDropRequest.responseText.substr(0,4) == 'Error')
{
var langs = verifyDropRequest.responseText.substr(6);
alert("The destination Category does not support some languages used in this item.\nPlease install the following languages in this category:\n"+langs);
return false;
}else{
return true;
}
}
}

verifyDropRequest.send(null);

}


The drop never completes if there is an error or not.
the calling function:

function verifyDrop(item, overDrop)
{
itemId = item.id.substring(0,3);
dropId = overDrop.getAttribute('cat_id');
verifyDropRequest = new xmlRequest();

if(verifyLang(dropId, itemId) == true)
{
if(itemId == 'bro')
{
itemNo = dragItem.getAttribute('cat_id');
itemType = 'category';
var childUls = new Array();
var tmpId = null;
childUls = item.getElementsByTagName('LI');

for(i=0; i<childUls.length; i++)
{
tmpId = childUls[i].getAttribute('cat_id');
if(tmpId == 0){tmpId = "0";}
if(tmpId == dropId)
{
alert('You cannot move an item into\na descendant of itself!\n'+itemNo+' -> '+dropId);
return false;
}

}
if(itemNo == dropId)
{
alert('You cannot move an item into itself');
return false;
}
if(itemNo == 0){itemNo = "Top Category";}
var res = confirm('copying Category no. '+itemNo+' to category '+dropId);
return res;
}
else if(itemId == 'pg_')
{
itemNo = dragItem.getAttribute('pg_id');
itemType = 'page';
var res = confirm('copying page no. '+itemNo+' to category '+dropId);

return res;
}
}else{
return false;
}


}


There is obviously a flaw in my logic somewhere but damned if I can find it.
This is my first attempt a real client-side scripted site. Javascript and ajax are new toys for me. Is it even possible to base conditional logic on responseText?
OK stupid me I had set the first condition as

if((verifyDropRequest.readyState == 4)&&(verifyDropRequest.status != 200))

mistake. it never fires. I have changed that to

if((verifyDropRequest.readyState == 4)&&(verifyDropRequest.status == 200))

same problem.

A1ien51
06-08-2007, 02:38 PM
With Firefox, install the firebug extension and watch the XHR request and see what happens.

Eric

friscofrankie
06-08-2007, 03:08 PM
Perhaps I should add I am using Firebug and JavaScript Debugger (not together).
responseText

Error
//^ the string I am looking for

array(4) {

["verify_mv"]=>

string(8) "category"

["param"]=>

string(3) "7:3"

["FontSize"]=>

string(1) "1"

["PHPSESSID"]=>

string(32) "035d3a7e2fc56bc627a629bd2c0846b0"

}

<br />

verify_mv => category<br />

param => 7:3<br />

FontSize => 1<br />

PHPSESSID => 035d3a7e2fc56bc627a629bd2c0846b0<br />


responseHeader

Date Fri, 08 Jun 2007 13:41:09 GMT
Server Apache/2.0.54 (Unix) mod_ssl/2.0.54 OpenSSL/0.9.7e PHP/4.4.4 mod_perl/2.0.1 Perl/v5.8.6
X-Powered-By PHP/4.4.4
Content-Length 317
Keep-Alive timeout=15, max=100
Connection Keep-Alive
Content-Type text/html
Content-Language en

friscofrankie
06-29-2007, 01:45 PM
:D couple of problems with the above code:
Easy one first:

if(verifyDropRequest.responseText.substr(0,4) == 'Error')

Now if we count the characters in 'Error' huh... OK. Changed that to a "5" no joy.

For anyone else that needs to verify against a DB before proceeding with the remainder of their code (a situation where a synchronous call would be necessary) here is the fix.

verifyDropRequest.open("GET", queryString, false);
verifyDropRequest.send(null);
if((verifyDropRequest.readyState == 4)&&(verifyDropRequest.status == 200))
{
if(verifyDropRequest.responseText.substr(0,5) == 'Error')
{
var langs = verifyDropRequest.responseText.substr(7);
alert(verifyDropRequest.responseText.substr(0,6)+"\nThe destination Category does not support some languages used in this item.\nPlease install the following languages in this category:"+langs);
return false;
}
else if(verifyDropRequest.responseText.substr(0,2) == 'OK')
{
return true;
}
}


IN the previous code by setting up and event handler, the code had no reason to stop and wait. The return values never got a chance to be set and They were undefined in the calling function.
By setting the if/else structure after the send(); I forced the function to wait for the responseText.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum