473,324 Members | 2,254 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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=initXMLHTTPRequest();
if (req)
{
req.onreadystatechange=onReadyState;
req.open(HttpMethod,url,true);
req.send(null);
}
}
function initXMLHTTPRequest()
{
var xRequest=null;
if (window.XMLHttpRequest)
{
xRequest=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
xRequest=new ActiveXObject
("Microsoft.XMLHTTP");
}
return xRequest;
}
function onReadyState()
{
var ready=req.readyState;
var data=null;
if (ready==4)
{
if(req.status==200)
data=req.responseText;
}
else
{
data="loading...["+ready+"]";
}
toConsole(data);
}
function toConsole(data)
{
if (console!=null)
{
var newline=document.createElement("div");
console.appendChild(newline);
var txt=document.createTextNode(data);
newline.appendChild(txt);
//console.innerHTML=data;
}
}
function abc()
{
console=document.getElementById("console");
sendRequest("ajax2.jsp?name="+encodeURIComponent(u ser.value));
}
</script>
</head>
<body>
<p>
<input type="text" value="" name="user" onchange="javascript:abc()"></p>
<div id="console"></div>

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



& this is my jsp code
Expand|Select|Wrap|Line Numbers
  1. <%
  2. response.setContentType("text/html");
  3. response.setHeader("Cache-Control","no-Cache");
  4. response.getWriter().write(=request.getParameter("user"));
  5. response.getWriter().write("Tejashri");
  6. %>
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
Mar 16 '08 #1
11 3492
gits
5,390 Expert Mod 4TB
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
Mar 16 '08 #2
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
Mar 17 '08 #3
gits
5,390 Expert Mod 4TB
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
Mar 17 '08 #4
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
Mar 18 '08 #5
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
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type="text/javascript" src="simple.js">
  4. </script>
  5. </head>
  6. <body>
  7. <p>
  8. <input type="text" name="name" value="" onchange = abc()>
  9. <div id="simple_div"></div></p>
  10. </body>
  11. </html>
  12.  
Javascript CODE
Expand|Select|Wrap|Line Numbers
  1.  var req=null;
  2. var console=null;
  3. function sendRequest(url,HttpMethod)
  4. {    
  5.     if (!HttpMethod)
  6.     {
  7.         HttpMethod="GET";
  8.     }
  9.     req=initXMLHTTPRequest();
  10.     if (req)
  11.     {
  12.         req.open(HttpMethod,url,true);
  13.         req.onreadystatechange=onReadyState;
  14.         req.send(null);
  15.     }
  16. }
  17. function initXMLHTTPRequest()
  18. {
  19.     var xRequest=null;
  20.     if (window.XMLHttpRequest)
  21.     {
  22.     xRequest=new XMLHttpRequest();
  23.     } 
  24.     else if (window.ActiveXObject)
  25.     {
  26.     xRequest=new ActiveXObject("Microsoft.XMLHTTP");
  27.     }
  28.     return xRequest;
  29. }
  30. function onReadyState()
  31. {
  32.     var ready=req.readyState;
  33.     var data="sad";
  34.     if (ready==4)
  35.     {
  36.         if(req.status==200)
  37.         {
  38.             data=req.responseText;
  39.         }
  40.     }
  41.     else
  42.     {
  43.         data="loading...["+ready+"]";
  44.     }
  45.     toConsole(data);
  46. }
  47. function toConsole(data)
  48. {
  49.     if (console!=null)
  50.     {
  51.  
  52.         var newline=document.createElement("div");
  53.         console.appendChild(newline);
  54.         var txt=document.createTextNode(data);
  55.         newline.appendChild(txt);
  56.  
  57.     }
  58. }
  59. function abc()
  60. {
  61.     console=document.getElementById("simple_div");
  62.     sendRequest("http://localhost:8080/bit/simple.jsp","GET");
  63. }
JSP code
Expand|Select|Wrap|Line Numbers
  1. <%
  2. String abcd=request.getParameter("name");
  3. response.setContentType("text/html");
  4. response.setHeader("Cache-Control","no-cache");
  5. response.getWriter().write(abcd);
  6. %>
Thanks in advance
Mar 19 '08 #6
gits
5,390 Expert Mod 4TB
try to change the following things to this:

Expand|Select|Wrap|Line Numbers
  1. function sendRequest(url, HttpMethod) {    
  2.     if (!HttpMethod) {
  3.         HttpMethod="GET";
  4.     }
  5.  
  6.     var req = initXMLHTTPRequest();
  7.  
  8.     if (req) {
  9.         req.open(HttpMethod, url, true);
  10.         req.onreadystatechange = onReadyState;
  11.         req.send(null);
  12.     }
  13. }
  14.  
  15. function onReadyState(req) {
  16.     var ready = req.readyState;
  17.     var data = "sad";
  18.  
  19.     if (ready==4) {
  20.         if(req.status==200) {
  21.             data=req.responseText;
  22.         }
  23.     } else {
  24.         data="loading...["+ready+"]";
  25.     }
  26.  
  27.     toConsole(data);
  28. }
  29.  
kind regards
Mar 19 '08 #7
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
Mar 24 '08 #8
gits
5,390 Expert Mod 4TB
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:

Expand|Select|Wrap|Line Numbers
  1. req.onreadystatechange = function(res) {
  2.      return onReadyState(res);
  3. }(req);
kind regards
Mar 24 '08 #9
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
Mar 25 '08 #10
HI gits I got one solution,I mean Now it prints text.Text which I declared in JSP means
response.getWriter().write("HI This is JSP Text");

Now problem is that how to pass parameters to jsp,
means I have simple.html,simple.js,simple.jsp, & 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
Mar 25 '08 #11
gits
5,390 Expert Mod 4TB
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
Mar 25 '08 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Luis | last post by:
Is there an easier way to check if any of the fields returned in a select statement have null values? After running this command: set rs = conn.execute("select A,B,C,D,E,F,G,H,I,J from...
3
by: Shawn | last post by:
Hey there i'm trying to post a section of XML code to an XMLSocket Server via ASP I can successfully connect to the server, but it won't disconnect because the string needs a null character...
3
by: Danny Tuppeny | last post by:
Hi all, I'm trying to send a null character as a string delimiter through a TcpClient (code below). It's to connect to this poker bot room: http://games.cs.ualberta.ca/webgames/poker/bots.html...
4
by: kyl | last post by:
A Java Axis consumed by .Net C# client, it built and executed the WS method from the WSDL gotten from OK, the response object is NULL. Is there any more thing more should be done on Java server?
9
by: MSDNAndi | last post by:
Hi, I have a set of simple webservices calls that worked fine using .NET Framework 1.0. I am calling a Java/Apache based webservices, the calling side is not able to supply a proper WSDL. ...
7
by: news | last post by:
..Net 2.0 Hi, I'm getting a nullreferenceexception when calling a function from a worker thread: private void CallbackProc(string response, Exception ex) { if (this.InvokeRequired) {...
2
by: Uldis Bojars | last post by:
Hi All, I have encountered problems with JS RegExp.exec() and can't find what is the problem. Could you help me? formRequest is a function that extracts some information from XMLHTTPRequest...
0
by: alister7 | last post by:
hi every1 im trying to download a music file from the database SQLserver.which i upload in the database.. The Code below works fine in wen i create a new project of an ASP.Net web application..m...
2
by: crabsdf | last post by:
My project is a single method class which takes a xml object and, using Apache FOP, transforms it into a PDF which is returned as an output stream. Here is full code package...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.