Thursday, September 27, 2007

Ajax problems & solutions

Big whoooops!!
It turned out that problem with prototype was not client's but server's. Server refuged to get data because it thought that they were dangerous.
Solution: ValidateRequest="false" on the page
But then was another bigger trouble - how to fill iframe by data from database? There were a few ways:
1. Load from page when ifram page is loading (through iframe's attributes or Javascript's var) - this way is very unreliable and doesn't work in most cases).
2. Load as XML from page and parse into DOM then. Alas! The content of iframe can have absolutely any structure so the using of XML is impossible.
3! Load using XMLHttpRequest into iframe directly immediately after loading of the page:

function GetData()
{
var url = 'NodeContentProcess.aspx';
var params = {nodeId:nodeId, action:'getxmlcontent'};
var request = new Ajax.Request(
url,
{
encoding: 'UTF-8',
method: 'post',
contentType: 'application/x-www-form-urlencoded',
parameters: params,
onSuccess: GetSuccess
});
}
function GetSuccess(request)
{
var xmlDoc = request.responseText;
if ($(editFrame).contentDocument)
$(editFrame).contentDocument.body.innerHTML = xmlDoc;
else
$(editFrame).contentWindow.document.body.innerHTML = xmlDoc;
}


This way is fast and elegant but has one significant defect - it requires one more request to the server. The solution I think is very simple: I will not load a separate page into iframe but have only div with iframe and load data into it after a clik on node.
TODO: I will do that later, really.

Time spent: 6 hours

No comments: