Help needed on JavaScript and Ajax..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DurgaKar1780
    New Member
    • Feb 2007
    • 2

    Help needed on JavaScript and Ajax..

    Hi All,
    I have an requirement in my project, where i have to show a pop up with an moving icon when a button is clicked. But the problem is that the pop up should close automatically once we get the response from webservice. So i wanted to go with ajax as i far as my knowledge goes we also wanted asynchronous calls to the server side code to know whether the response has come or not.
    I am using JSF and opening the pop from java code.Then I have a java script which calls the ajax function[to create objects -xmlHttpObject]. From the ajax function i am trying to call a servlet where i am actually writing the business logic.If the logic completes successfully without any exceptions that means the response is returned and i should get readyState as 4.Then i can do a self.close() in the ajax function to close the pop up. But unfortunately I dont know what is going wrong in this approach.

    Some one please guide me. I need some suggestions so that i can implement it.
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Welcome to The Scripts.

    The method you have described should work fine. Please post your code (in code tags).

    Comment

    • DurgaKar1780
      New Member
      • Feb 2007
      • 2

      #3
      Originally posted by acoder
      Welcome to The Scripts.

      The method you have described should work fine. Please post your code (in code tags).
      Hi acorder,
      Please find the code as followa :
      1] My JSF page has a command button which has an action attribute and invokes a metod in the backing bean where I open a pop up.Please find the code to open pop up as follows -

      String windowWidth = "575";
      String windowHeight = "250";
      html.append("<H TML>");
      html.append("<T ITLE></TITLE>");
      html.append("<H EAD>");
      html.append("<S CRIPT LANGUGE=\"JavaS cript\">");
      html.append("fu nction windowOpen(){") ;
      html.append("wi ndow.open('/web-swaf/faces/steel247/dh/jspf/Sendrequest.jsp ','RequestScope ',"
      + "'left=200,top= 250,height="
      + windowHeight
      + ",width="
      + windowWidth
      + ",location=no,s crollbars=no,"
      + "menubars=no,to olbars=no,resiz able=yes');");
      html.append("fo rm2.submit();") ;
      html.append("}" );
      html.append("</SCRIPT>");
      html.append("</HEAD>");
      html.append("<B ODY onLoad=\"window Open()\">");
      html.append("<f orm name=\"form2\"> ");
      html.append("</form>");
      html.append("</BODY>");
      html.append("</HTML>");

      2] Now in the jsp - Sendrequest.jsp i do the following -

      <SCRIPT language="javas cript1.1">ajaxF unction()</SCRIPT>
      <SCRIPT>
      function ajaxFunction()
      { var xmlHttp;
      alert("in ajax function");
      try
      {
      xmlHttp=new XMLHttpRequest( ); }
      catch (e)
      {
      try
      { xmlHttp=new ActiveXObject(" Msxml2.XMLHTTP" ); }
      catch (e)
      { try
      { xmlHttp=new ActiveXObject(" Microsoft.XMLHT TP"); }
      catch (e)
      { alert("Your browser does not support AJAX!");
      return false; } } }

      alert("Before action methods");
      xmlHttp.open("G ET","/RequestScopeSer vlet",true);
      xmlHttp.onready statechange = callback;
      xmlHttp.send(nu ll);
      alert("After action methods");

      }

      function callback() {
      switch (xmlhttp.readyS tate){
      case 1:
      alert("Inside onLoading 1");
      break;
      case 2:
      alert("Inside onLoaded 2");
      break;
      case 3:
      alert("Inside onInteractive 3");
      break;
      case 4:
      this.parent.ref resh();
      alert("Inside Complete 4");
      me.close();
      break;

      }

      }
      </SCRIPT>

      It should enter the forth Case and close the pop up. but my servlet is not invoked where i am executing my business methods.

      Thanks in advance for any inputs on this.

      Comment

      Working...