get null response

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jomshri
    New Member
    • Mar 2008
    • 7

    get null response

    hi
    This my ajax code
    [HTML]<html>
    <head>
    <script type="text/javascript">
    var req=null;
    var console=null;
    function sendRequest(url ,HttpMethod)
    {
    if (!HttpMethod)
    {
    HttpMethod="GET ";
    }
    req=initXMLHTTP Request();
    if (req)
    {
    req.onreadystat echange=onReady State;
    req.open(HttpMe thod,url,true);
    req.send(null);
    }
    }
    function initXMLHTTPRequ est()
    {
    var xRequest=null;
    if (window.XMLHttp Request)
    {
    xRequest=new XMLHttpRequest( );
    }
    else if (window.ActiveX Object)
    {
    xRequest=new ActiveXObject
    ("Microsoft.XML HTTP");
    }
    return xRequest;
    }
    function onReadyState()
    {
    var ready=req.ready State;
    var data=null;
    if (ready==4)
    {
    if(req.status== 200)
    data=req.respon seText;
    }
    else
    {
    data="loading.. .["+ready+"]";
    }
    toConsole(data) ;
    }
    function toConsole(data)
    {
    if (console!=null)
    {
    var newline=documen t.createElement ("div");
    console.appendC hild(newline);
    var txt=document.cr eateTextNode(da ta);
    newline.appendC hild(txt);
    //console.innerHT ML=data;
    }
    }
    function abc()
    {
    console=documen t.getElementByI d("console");
    sendRequest("aj ax2.jsp?name="+ encodeURICompon ent(user.value) );
    }
    </script>
    </head>
    <body>
    <p>
    <input type="text" value="" name="user" onchange="javas cript:abc()"></p>
    <div id="console"></div>

    </body>
    </html>[/HTML]



    & this is my jsp code
    [CODE=java]<%
    response.setCon tentType("text/html");
    response.setHea der("Cache-Control","no-Cache");
    response.getWri ter().write(=re quest.getParame ter("user"));
    response.getWri ter().write("Te jashri");
    %>[/CODE]

    After browsing It should display
    Whatever text is entered in text field & then Tejashri
    But it gives sometimes "null", sometimes "Tejashri", sometimes some other combination
    Please check this code If u fill it is wrong please intinate me or correct that code
    thanks in advance
    Last edited by gits; Mar 16 '08, 04:15 PM. Reason: added code tags
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5388

    #2
    may be the request.status doesn't get to 200? in that case you will get a null value (as you programmed it :: data = null) ... may be the request is aborted?

    kind regards

    Comment

    • jomshri
      New Member
      • Mar 2008
      • 7

      #3
      hi thanks for help
      but please tell me what should I do now.
      My project get stuck on this point


      please please help me
      Thanks in advance

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5388

        #4
        do you use your glaobal request object for other requests too? you could use a mechanism that avoids to send a new request before you got the response from the already started one or you may use a local request variable.

        kind regards

        Comment

        • jomshri
          New Member
          • Mar 2008
          • 7

          #5
          hi Gits
          thanks for ur quick reply everytime.
          Let me know ,what is the tomcat_home path in Environment variable.
          Where should we place Tools.jar file in tomcat folder
          Please give ans in detail
          I am new to Tomcat,JS
          So please help me
          I hope This time also u reply me as soon as possible
          --thanks

          Comment

          • jomshri
            New Member
            • Mar 2008
            • 7

            #6
            Actually It is not working yet.
            What should I do for that please suggest me something
            Again I am sending my code please check it carefully & let me know what r the errors in that code.

            HTML CODE
            [CODE=html]<html>
            <head>
            <script type="text/javascript" src="simple.js" >
            </script>
            </head>
            <body>
            <p>
            <input type="text" name="name" value="" onchange = abc()>
            <div id="simple_div" ></div></p>
            </body>
            </html>
            [/code]
            Javascript CODE
            [code=javascript]
            var req=null;
            var console=null;
            function sendRequest(url ,HttpMethod)
            {
            if (!HttpMethod)
            {
            HttpMethod="GET ";
            }
            req=initXMLHTTP Request();
            if (req)
            {
            req.open(HttpMe thod,url,true);
            req.onreadystat echange=onReady State;
            req.send(null);
            }
            }
            function initXMLHTTPRequ est()
            {
            var xRequest=null;
            if (window.XMLHttp Request)
            {
            xRequest=new XMLHttpRequest( );
            }
            else if (window.ActiveX Object)
            {
            xRequest=new ActiveXObject(" Microsoft.XMLHT TP");
            }
            return xRequest;
            }
            function onReadyState()
            {
            var ready=req.ready State;
            var data="sad";
            if (ready==4)
            {
            if(req.status== 200)
            {
            data=req.respon seText;
            }
            }
            else
            {
            data="loading.. .["+ready+"]";
            }
            toConsole(data) ;
            }
            function toConsole(data)
            {
            if (console!=null)
            {

            var newline=documen t.createElement ("div");
            console.appendC hild(newline);
            var txt=document.cr eateTextNode(da ta);
            newline.appendC hild(txt);

            }
            }
            function abc()
            {
            console=documen t.getElementByI d("simple_div") ;
            sendRequest("ht tp://localhost:8080/bit/simple.jsp","GE T");
            }[/CODE]

            JSP code
            [CODE=java]<%
            String abcd=request.ge tParameter("nam e");
            response.setCon tentType("text/html");
            response.setHea der("Cache-Control","no-cache");
            response.getWri ter().write(abc d);
            %>[/CODE]

            Thanks in advance
            Last edited by gits; Mar 19 '08, 08:46 AM. Reason: added code tags

            Comment

            • gits
              Recognized Expert Moderator Expert
              • May 2007
              • 5388

              #7
              try to change the following things to this:

              [CODE=javascript]function sendRequest(url , HttpMethod) {
              if (!HttpMethod) {
              HttpMethod="GET ";
              }

              var req = initXMLHTTPRequ est();

              if (req) {
              req.open(HttpMe thod, url, true);
              req.onreadystat echange = onReadyState;
              req.send(null);
              }
              }

              function onReadyState(re q) {
              var ready = req.readyState;
              var data = "sad";

              if (ready==4) {
              if(req.status== 200) {
              data=req.respon seText;
              }
              } else {
              data="loading.. .["+ready+"]";
              }

              toConsole(data) ;
              }
              [/CODE]
              kind regards

              Comment

              • jomshri
                New Member
                • Mar 2008
                • 7

                #8
                HI Gits
                Thanks for reply
                But that code also didn't run ,even it doesn't display anything I mean neither null nor correct value.Please reply me as soon as possible.

                thanks

                Comment

                • gits
                  Recognized Expert Moderator Expert
                  • May 2007
                  • 5388

                  #9
                  Originally posted by jomshri
                  HI Gits
                  Thanks for reply
                  But that code also didn't run ,even it doesn't display anything I mean neither null nor correct value.Please reply me as soon as possible.

                  thanks
                  hi ...

                  try this:

                  [CODE=javascript]req.onreadystat echange = function(res) {
                  return onReadyState(re s);
                  }(req);[/CODE]
                  kind regards

                  Comment

                  • jomshri
                    New Member
                    • Mar 2008
                    • 7

                    #10
                    Thanks for reply
                    But i didn't get what is that (res) i.e. res
                    Is that is used for response or for ajax variable
                    Please reply as soon as possible
                    thanks

                    Comment

                    • jomshri
                      New Member
                      • Mar 2008
                      • 7

                      #11
                      HI gits I got one solution,I mean Now it prints text.Text which I declared in JSP means
                      response.getWri ter().write("HI This is JSP Text");

                      Now problem is that how to pass parameters to jsp,
                      means I have simple.html,sim ple.js,simple.j sp, & I am using js as src in html. And jsp's path as my URL.Then How can I send value of Input Field, Text, which is in html.

                      please reply asap(i.e. as soon as possible)

                      Thanks in advance

                      Comment

                      • gits
                        Recognized Expert Moderator Expert
                        • May 2007
                        • 5388

                        #12
                        Originally posted by jomshri
                        Thanks for reply
                        But i didn't get what is that (res) i.e. res
                        Is that is used for response or for ajax variable
                        Please reply as soon as possible
                        thanks
                        hi ...

                        as you can see we call a function with req that is our request and pass it as a param to the funtion that we assign onreadystate-change to your onReadyState function ... it's a closure for the req-variable since the callback is executed async ... and has its own execution-context later on

                        kind regards

                        Comment

                        Working...