Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Why am I getting an empty string in my responseText?

 
Nathan Milota
Ranch Hand
Posts: 361
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



So, I want to access the data, but the var data is empty, so when I try to parse it, I get an error.   The this.responseText is generating the right output, but the xhr.responseText is not.   Why is it giving me nothing?
 
Stephan van Hulst
Saloon Keeper
Posts: 11488
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably because the server hasn't responded yet by the time you call xhr.responseText. That's why you need to check the response asynchronously in a callback function.
 
Nathan Milota
Ranch Hand
Posts: 361
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Probably because the server hasn't responded yet by the time you call xhr.responseText. That's why you need to check the response asynchronously in a callback function.




It's getting the right response when using the add event listener though.   Why is it not responding?  Nearly ever page through Google I found said this is how you get object data from a database, but it is just not working.
 
Nathan Milota
Ranch Hand
Posts: 361
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Probably because the server hasn't responded yet by the time you call xhr.responseText. That's why you need to check the response asynchronously in a callback function.



I thought the xhr.open was the callback function?
 
Bear Bibeault
Marshal
Posts: 67366
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. The callback function is the function that is passed as the event handler.
 
Nathan Milota
Ranch Hand
Posts: 361
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any hint here then?  An example of something that works keeping in mind I'm still new to javascript?
 
Stephan van Hulst
Saloon Keeper
Posts: 11488
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No.

When you call open(), you initialize the request. When you call send(), it gets sent to the server. It will take some time for the request to arrive at the server, be processed, and a response to be returned. That's why when you access responseText immediately after sending the request, it won't contain a value yet.

The solution is to register a callback function that gets called when the response has returned. You've done this with the call to addEventListener(). You need to process the response there
 
Nathan Milota
Ranch Hand
Posts: 361
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:No.

When you call open(), you initialize the request. When you call send(), it gets sent to the server. It will take some time for the request to arrive at the server, be processed, and a response to be returned. That's why when you access responseText immediately after sending the request, it won't contain a value yet.

The solution is to register a callback function that gets called when the response has returned. You've done this with the call to addEventListener(). You need to process the response there



The only thing I've noticed differently that my code doesn't have is the status part

if(this.readyState === 4 && this.status===200)


I thought maybe that may have had something to do with waiting for a response, but I still get the same problem
 
Stephan van Hulst
Saloon Keeper
Posts: 11488
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The difference is in when the code is called. The code after the send() is called immediately. The code in the listener is delayed until the response returns.

The code in the listener will do exactly what you want. It's also the proper way. Why not just use it and be done with it?
 
Nathan Milota
Ranch Hand
Posts: 361
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:The difference is in when the code is called. The code after the send() is called immediately. The code in the listener is delayed until the response returns.

The code in the listener will do exactly what you want. It's also the proper way. Why not just use it and be done with it?



So, are you saying I should put the send and JSON parse code inside the listener?
 
Nathan Milota
Ranch Hand
Posts: 361
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's probably not what you meant, as I tried that and I just get a bunch of nothing.
 
Stephan van Hulst
Saloon Keeper
Posts: 11488
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The listener is called when the response returns. So how can it ever be called when you haven't sent the request in the first place?

What I'm saying is, send the request outside the listener and let the function end. Put the code to handle the response in the listener.
 
Nathan Milota
Ranch Hand
Posts: 361
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:The listener is called when the response returns. So how can it ever be called when you haven't sent the request in the first place?

What I'm saying is, send the request outside the listener and let the function end. Put the code to handle the response in the listener.



So, I changed it to...




The error stopped, but it still is empty and the last line is not executing to give me the object of the json response.

The if statement is not executing it seems, so that's why the error went away, but there's nothing to show.  
 
Nathan Milota
Ranch Hand
Posts: 361
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my function now.   I'm still getting the same error.  

 
Nathan Milota
Ranch Hand
Posts: 361
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, so now it's returning the objects correctly, but I'm still getting the error for some reason.

The Unexpected end of JSON input error.  I don't know what it is doing wrong this time.  
 
Ron McLeod
Saloon Keeper
Posts: 3027
414
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is this old code still there?:
 
Nathan Milota
Ranch Hand
Posts: 361
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:Why is this old code still there?:



I deleted it after I noticed it myself.   It's not there.


However, I'm still getting an error on the parse, as it's not parsing correctly for some reason, even though it shows all my objects correctly.
 
Nathan Milota
Ranch Hand
Posts: 361
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The curly braces are positioned properly, the output looks fine with the json format, but somehow there is still an error on the parse that I can't make go away.
 
Ron McLeod
Saloon Keeper
Posts: 3027
414
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nathan Milota wrote:The curly braces are positioned properly...



Shouldn't your result-handling code be located inside the code block which gets executed when the result is available?
 
Ron McLeod
Saloon Keeper
Posts: 3027
414
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, I'm not sure if it really matters or not, but I would call send() after registering the event listener, not before.
 
Nathan Milota
Ranch Hand
Posts: 361
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:

Nathan Milota wrote:The curly braces are positioned properly...



Shouldn't your result-handling code be located inside the code block which gets executed when the result is available?



Okay, that seems to have fixed it.  I thought I tried that but it didn't work before, but maybe I did something differently.
 
Stephan van Hulst
Saloon Keeper
Posts: 11488
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So how does your code look now?
 
Nathan Milota
Ranch Hand
Posts: 361
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:So how does your code look now?




That part of the code is working.   However, I've stopped as of now because I for some reason cannot map a foreign key or even post an int to any of my tables due to it either being an SQLGrammarException or something like..  no int/Int-argument constructor/factory method to deserialize from Number value

I'm just getting too frustrated.
 
Dave Tolls
Rancher
Posts: 4493
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There;s no "something like that".  There will be specific errors which you can copy and paste here which will allow us to explain the issue to you.
Don't try and interpret or summarise the error to us since you don't understand enough to know which bits are important and which aren't.
 
Nathan Milota
Ranch Hand
Posts: 361
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:There;s no "something like that".  There will be specific errors which you can copy and paste here which will allow us to explain the issue to you.
Don't try and interpret or summarise the error to us since you don't understand enough to know which bits are important and which aren't.



I'm abandoning this project now, because it is becoming to complex for the skills I have at this time, and I have graduate studies that are more important now.

However, what I was continuously getting is an SQLGrammarException when trying to post an int to MySQL with a post method.  It was expecting an int, I put a number it, it failed.  
 
Dave Tolls
Rancher
Posts: 4493
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's precisely what I meant by "interpret or summarise the error".
The actual error, along with the SQL statement and parameters, would allow us to try and fix the problem.
MySQL won't be lying about the problem.

But if you're abandoning it then OK.
 
Nathan Milota
Ranch Hand
Posts: 361
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:That's precisely what I meant by "interpret or summarise the error".
The actual error, along with the SQL statement and parameters, would allow us to try and fix the problem.
MySQL won't be lying about the problem.

But if you're abandoning it then OK.



I'm not giving up, but I'm just going to start off with a smaller project when I get the time, as a lot just isn't working and I cannot figure out why, meaning I haven't really learned the basics much yet.
 
A feeble attempt to tell you about our stuff that makes us money
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!