In my last post I was having a problem js not waiting for each request to finished before continue. After a bit of searching I finally found my problem. I was trying to solve a synchronous problem asynchrounously. So I just change the request to synchronous calls and it fixed my problem.
My Other Project
Right now in my spare time I am working on a course planning program that will make its a lot more user friendly when I am planning out my courses for semesters. Right now I have a screen scraping script that grabs all the class info for each semester. and I am working on a front end in js that will make it nice to select classes.
Saturday, May 17, 2008
Friday, May 16, 2008
A month later
A lot of development has occurred since the last post. I am no longer using prototype as it did not suit my needs. Now I am using the awesome framework mootools which looks like it share a bit with prototype. I solved the predicament about contacting a remote server with ajax from the previous post. The problem with how I was trying to solve it before is that xmlhttprequest() can only be called from the same server the script is located on. Trying to access another page is called Cross Domain and xmlhttprequest does not do this at the moment. To get around this I made ajax contact a local script in php and then the php would contact the site on a different domain using cURL. This made contacting the site a lot easier because cURL makes it ridiculously easy to contact pages especially with special parameters like Http Authentication and SSL encryption. Anyways I got that working and then I had to convert the php script into Java. Since the standard development language at work is Java.
Java Problems
This was a bigger pain then I realized. The Java web server I am using is Tomcat and I am using struts 2 as the framework to create pages. The version of curl for java is under developed so for what I needed it to do it would not cut so I had to find a different solution. I was thinking that maybe the standard api for java would have something but once again it was also not fully developed. I finally came across Apache Jakarta Project HttpClient package. This saved the day and I got it contacting the remote server through Http Authentication using NTLM and on through https. I would like to note that JBDC owns. Getting my script to connect to a db2 server was a pain in php I had to set up a obdc connection and download a good amount of ibm software. With Java all u need is the jar with the DB2 drivers and the use the jdbc to contact the server.
XML Problems
Another interesting problem was that parsing the xml I would get back from the site. Java has some generic xml packages but I couldn't find any information on how to use them. So I solved this problem with Xerces-J another Apache project that is a package used to parse xml. I used a SAX parser to get the info from the xml.
Current Problem (waiting for ajax response inside a timed interval)
So everything is working fine now but there is a bug that I am trying to solve which is driving me crazy with the ajax. Essentially what I am trying to do is make a function that ask the server if new information is available in a given interval. But if the request takes longer than the interval I want it to wait for it to get the response from the server. Right now it just ask every interval regardless if it gets a response back. So to solve this dilemma, I thought why not make a while loop that checks if the request is running. If it is, continue waiting in the loop. Else, go back to normal operation. So what I came up with is something like:
Java Problems
This was a bigger pain then I realized. The Java web server I am using is Tomcat and I am using struts 2 as the framework to create pages. The version of curl for java is under developed so for what I needed it to do it would not cut so I had to find a different solution. I was thinking that maybe the standard api for java would have something but once again it was also not fully developed. I finally came across Apache Jakarta Project HttpClient package. This saved the day and I got it contacting the remote server through Http Authentication using NTLM and on through https. I would like to note that JBDC owns. Getting my script to connect to a db2 server was a pain in php I had to set up a obdc connection and download a good amount of ibm software. With Java all u need is the jar with the DB2 drivers and the use the jdbc to contact the server.
XML Problems
Another interesting problem was that parsing the xml I would get back from the site. Java has some generic xml packages but I couldn't find any information on how to use them. So I solved this problem with Xerces-J another Apache project that is a package used to parse xml. I used a SAX parser to get the info from the xml.
Current Problem (waiting for ajax response inside a timed interval)
So everything is working fine now but there is a bug that I am trying to solve which is driving me crazy with the ajax. Essentially what I am trying to do is make a function that ask the server if new information is available in a given interval. But if the request takes longer than the interval I want it to wait for it to get the response from the server. Right now it just ask every interval regardless if it gets a response back. So to solve this dilemma, I thought why not make a while loop that checks if the request is running. If it is, continue waiting in the loop. Else, go back to normal operation. So what I came up with is something like:
refresh.periodical(1000ms,this)
function refresh{
stop the timed function calls
make ajax call
while(ajax.running==true){
}
start timed function calls again
}
The only problem with this is that javascript is single threaded so the while loop takes over and doesn't let anything else happen. in fact it caused the browser to freeze up. So i need to figure out a way to resolve this situation. It's a bit annoying that when javascript sends a request it doesn't wait for it to get a response it just continues executing.
Subscribe to:
Comments (Atom)