aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Getting data from the server with Ajax Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Getting data from the server with Ajax" Watch "Getting data from the server with Ajax" New topic
Author

Getting data from the server with Ajax

Bill Hamberg
Greenhorn

Joined: Nov 18, 2013
Posts: 16
Finally think I have this 87.5% figured out - found the code and adapted it.

Not sure I fully understand what's happening here but it does work.

If someone could comment it out, I'm sure there are some of us that would like to understand it better (and possibly make it more efficient.)
(also found a JQuery version but could not get it working.)

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61002
    
  65

I think you'd be better off pursuing the jQuery version. Dealing with cross-browser Ajax is just too much trouble to bother with.

All of that would reduce to a line or two with jQuery.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bill Hamberg
Greenhorn

Joined: Nov 18, 2013
Posts: 16
Bear Bibeault wrote:I think you'd be better off pursuing the jQuery version. Dealing with cross-browser Ajax is just too much trouble to bother with.

All of that would reduce to a line or two with jQuery.


can you share (how to code) the JQuery version, for us novices??
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61002
    
  65

Sure I can get you started with some skeleton code. To load an HTML fragment into the DOM:
Yes, that's all.

For a more general get, such as you appear to be doing:
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61002
    
  65

Btw, why XML? It's painful to deal with. JSON is much preferred when returning data via Ajax.
Bill Hamberg
Greenhorn

Joined: Nov 18, 2013
Posts: 16
Bear Bibeault wrote:Btw, why XML? It's painful to deal with. JSON is much preferred when returning data via Ajax.


Right now it's the easiest to implement. I can have a VA take the XML template and just update the info and FTP to subdirectory.

My ultimate goal - which you mentioned before - is to have a DB on the back end (which would also eliminate the need for subdirectories and duplicating all the HTML & images files??) and have all users access the site via a unique URL (ie. www.kwmds.com?User#1), then have the "same" page served dynamically.

Based on what I'm reading here and on other sites I'm guessing its relatively easy to implement - IF YOU KNOW WHAT YOU'RE DOING - and of course I'm starting at ground zero
Bill Hamberg
Greenhorn

Joined: Nov 18, 2013
Posts: 16
Bear Bibeault wrote:Sure I can get you started with some skeleton code. To load an HTML fragment into the DOM:
Yes, that's all.

OK - breaking this down for the newbie...

(#idOfParent) = the ID my HTML is using?
.load = jQuery method
('/url/to/resource') = link to my XML file?

Question 1 - how do I pass the fact that I only need the "agent" tag?
Question 2 - "agent" has 6 lines of data. Will the .load method return all 6 or do i need iterate through?

version of the XML file below...

Bill Hamberg
Greenhorn

Joined: Nov 18, 2013
Posts: 16
Bill Hamberg wrote:
Bear Bibeault wrote:Sure I can get you started with some skeleton code. To load an HTML fragment into the DOM:
Yes, that's all.

OK - breaking this down for the newbie...

(#idOfParent) = the ID my HTML is using?
.load = jQuery method
('/url/to/resource') = link to my XML file?

Question 1 - how do I pass the fact that I only need the "agent" tag?
Question 2 - "agent" has 6 lines of data. Will the .load method return all 6 or do i need iterate through?


WOW - try and you shall do!semi-works; just need to break the data into separate lines!?

(this friggin JQuery stuff is cool! )
Think we just eliminate 25 lines of code?!?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61002
    
  65

I would once again question the XML. I tend to make changes on the server in order to make things easier on the client, rather than the other way around.

But, once you get an XML document back as the data, you can use jQuery to select from the XML DOM the same way as the HTML DOM. For example: $('agent',xmlDoc).text()

Granted, I've never done that (I use XML for many things, but returning it to a client as an Ajax response would never be one of them) so there may be nuances I've missed.
Bill Hamberg
Greenhorn

Joined: Nov 18, 2013
Posts: 16
Bear Bibeault wrote:I would once again question the XML...


It was the easiest (shortest learning curve) path at the time.

Bill Hamberg wrote:My ultimate goal - which you mentioned before - is to have a DB on the back end (which would also eliminate the need for subdirectories and duplicating all the HTML & images files??) and have all users access the site via a unique URL (ie. www.kwmds.com?User#1), then have the "same" page served dynamically.

Based on what I'm reading here and on other sites I'm guessing its relatively easy to implement - IF YOU KNOW WHAT YOU'RE DOING - and of course I'm starting at ground zero


So now that that is working what resources would you recommend (reading/hiring) to implement the database solution?

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61002
    
  65

Any decent book on web development. Head First Servlets and JSP is good (disclaimer: I was a technical reviewer).

You might also want to read these articles to understand what JSP is all about and how to structure traditional Java web apps:
  • The Secret Life of JSPs
  • The Front Man


  • For JavaScript, there's my own Secrets of the JavaScript Ninja and though my jQuery book (jQuery in Action (2nd Ed)) is based on an older version of jQuery, all the info in it is still good enough to get you going.
    Bill Hamberg
    Greenhorn

    Joined: Nov 18, 2013
    Posts: 16
    THANKS!!!
     
    wood burning stoves
     
    subject: Getting data from the server with Ajax