Texis URL in javascript

barry.marcus
Posts: 288
Joined: Thu Nov 16, 2006 1:05 pm

Texis URL in javascript

Post by barry.marcus »

I'm trying to implement AJAX in a script, and since I am pretty much a neophyte at it, it's not going smoothly and something just isn't working right. I found some javascript on the internet and I'm using it to call a vortex function. The function generates an XML string, which the javascript will then parse and use to dynamically update the page. But the vortex function is not getting called, so I'm doing something wrong. Here is the javascript so far:

<verb noesc><script language="JavaScript"></verb>
function sortDisplay(whichSort) {

var xmlhttp;
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.open("GET","$url/BuildCellDisplayXML.html",false);
xmlhttp.send();

var xmlDoc=xmlhttp.responseXML;
var txt="";
var x=xmlDoc.getElementsByTagName("Title");

<verb noesc>
for (i=0;i<x.length;i++) {
txt=txt + x[i].childNodes[0].nodeValue;
}
</verb>

}
</script>

The name of the vortex function to call is obviously BuildCellDisplayXML. For right now, I was simply going to display the value of txt somewhere on the page. Eventually it will update an already rendered HTML table. But like I said, the function is not even being called.

Related question... Once the above issue is resolved, I don't quite get how to pass the return value of the function (i.e., the XML string that the function builds) back to the responseXML of the xmlhttp object in the javascript. (I haven't gotten that far!) As I said, the javascript you see is something I just plucked from the internet.
barry.marcus
Posts: 288
Joined: Thu Nov 16, 2006 1:05 pm

Texis URL in javascript

Post by barry.marcus »

BTW, the "signature" of the vortex function looks like this:

<a name=BuildCellDisplayXML PUBLIC>

... code to build an XML string is here ...

</a>
User avatar
John
Site Admin
Posts: 2597
Joined: Mon Apr 24, 2000 3:18 pm
Location: Cleveland, OH
Contact:

Texis URL in javascript

Post by John »

A few thoughts:

There are a number of javascript frameworks out there that can make coding easier, e.g. jQuery.

You might want to make the URL be BuildCellDisplayXML.xml instead of .html so you get the right content type on the result.

If you check the web server log do you see if it is fetching the right URL?

Are you calling the javascript function sortDisplay?
John Turnbull
Thunderstone Software
User avatar
mark
Site Admin
Posts: 5514
Joined: Tue Apr 25, 2000 6:56 pm

Texis URL in javascript

Post by mark »

Also, view the source of the page containing the javscript to see that everything came out as expected. And use the browser's javascript console to see if there are any errors generated by the javascript.

The XML is returned to the javascript when the vortex function finishes, just like an html page. From the looks of it, the txt variable is supposed to end up with the text of the result. You'd then use more javascript to update the content of some element on the page.
barry.marcus
Posts: 288
Joined: Thu Nov 16, 2006 1:05 pm

Texis URL in javascript

Post by barry.marcus »

John, yes the function sortDisplay is getting called. I put an alert(); in the function and that is displaying. The function is called when the user clicks certain elements on the page (specifically, the headers of an HTML table.) Also, I changed the extension to "XML" from "HTML" as you suggested, though that did not resolve the issue.

Mark, I looked at the page source, and as expected, $url/BuildCellDisplayXML.xml resolves to /texiscgi/texis.exe/crosshairs/BuildCellDisplayXML.xml. Thinking that that *relative* URL may not be correct given that the client is sending that URL to the server, I modified the open function in the script to read as follows:

xmlhttp.open("GET","http://usnet2.inspherion.us:251$url/BuildCellDisplayXML.xml",false);

http://usnet2.inspherion.us:251 is the base absolute URL of the server on which I am developing this code. Looking at the page source again, the above string resolves to http://usnet2.inspherion.us:251/texiscg ... layXML.xml which appears to be correct. In spite of that the javascript function sortDisplay still does not seem to call the vortex function BuildCellDisplayXML. When I paste that full absolute URL into the browser, the vortex function IS called.

Perhaps the combination of the open() method followed by send() for the XMLHttpRequest object is not correct for a "GET". I understand that the open method is typically used asynchronously (i.e., the third parameter is usually set to true) but that synchronous usage will work ok (albeit, not entirely in the intended spirit of AJAX), especially if the return is very quick, which it should be in this test case. (The BuildCellDisplayXML function right now simply returns a short, trivial xml string.) My understanding is that in that case (i.e., when used synchronously), there is no need to determine the state change of the xmlHttpRequest or anything like that, which my code is not doing. In any case, as I said, it doesn't even seem like the vortex function is being called, so I suspect that asynchronous vs. synchronous is not my issue.

I'm stumped. Is there a simple example available somewhere that demonstrates how javascript on a vortex page can be used to call another vortex function for data in an AJAX context?
barry.marcus
Posts: 288
Joined: Thu Nov 16, 2006 1:05 pm

Texis URL in javascript

Post by barry.marcus »

And yes, I looked at the web server log... There is no evidence of a request containing BuildCellDisplayXML.
User avatar
John
Site Admin
Posts: 2597
Joined: Mon Apr 24, 2000 3:18 pm
Location: Cleveland, OH
Contact:

Texis URL in javascript

Post by John »

Debugging javascript snippets can be tough. What I'd be tempted to do to help is first, create a javascript variable with the url that you can do an alert on to see if it is correct, e.g.

var theurl = "http://usnet2.inspherion.us:251$url/BuildCellDisplayXML.xml";
alert("Url: " + theurl);

to make sure the url is correct, although you should just be able to use BuildCellDisplayXML.xml as the URL as a relative URL as long as you have either a trailing slash or function.extension on your URL. That should help avoid any cross-site errors.

Also check what you xmlhttp status and readyState are after the send.
John Turnbull
Thunderstone Software
User avatar
mark
Site Admin
Posts: 5514
Joined: Tue Apr 25, 2000 6:56 pm

Texis URL in javascript

Post by mark »

Here's some code extracted from a real life app. HTH

<a name=moremsgjs>
<verb noesc>
<script language=javascript>
function moremsg(First,Count){
var data;
var xmlhttp=new XMLHttpRequest();
</verb>
data="id=<fmt "%U" $id>&First="+First+"&Count="+Count;
<verb noesc>
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4){
if(xmlhttp.status==200){
document.getElementById("moremsgscontainer").innerHTML=xmlhttp.responseText;
document.body.style.cursor="auto";
}else{
// error, tell user
}
}
}
document.body.style.cursor="wait";
xmlhttp.open("GET","moremsg.html?"+data,true);
xmlhttp.send();
}
</script>
</verb>
</a>

<a name=moremsg public><!-- ajax callback -->
<$FirstMsg=$First>
<$MsgPerPage=$Count>
<showmsg>
</a>


<a name=main>
...
<moremsgjs>
<span id="moremsgscontainer">
<input type=button onclick="moremsg(1,$count)" value="All">
</span>
...
</a>
barry.marcus
Posts: 288
Joined: Thu Nov 16, 2006 1:05 pm

Texis URL in javascript

Post by barry.marcus »

Thanks Mark. I will work with this. Do you have code for the function showmsg? I tried:

<a name=showmsg public>
<sum "%s" $FirstMsg $MsgPerPage>
<return $ret>
</a>

but nothing displays for that.
User avatar
John
Site Admin
Posts: 2597
Joined: Mon Apr 24, 2000 3:18 pm
Location: Cleveland, OH
Contact:

Texis URL in javascript

Post by John »

Showmsg has to output something that will be seen by the Javascript, not just return a value.
John Turnbull
Thunderstone Software
Post Reply