View Full Version : Trying to display XML document in a table
09-26-2012, 11:22 PM
I'm trying to do what the title says, but it's apparently a lot more difficult than I thought. I'm using the following code:
var xHttp=new xhttprequest();
Right now the code is minimal, as I want to get a piece working first and adapt it into a fully fledged script. Note that I do have a <table id='search'> tag on the page itself, and it seems to work fine as long as I'm not dealing with an xml document like this.
Where am I going wrong? Thanks in advance.
09-27-2012, 12:12 AM
09-27-2012, 12:23 AM
Nothing seems to change.
09-27-2012, 01:32 AM
Well, actually there are several more things wrong.
For starters, this it totally bogus:
var xHttp=new xhttprequest();
It should be
var xHttp = new XMLHttpRequest(); // and watch out for upper/lower case! it DOES matter
And then you have a problem here:
getElementsByTagName returns an *ARRAY*. You can't take the firstChild of an array.
But none of this is going to work if your own site isn't "www.weebly.com".
There are several ways around this. The easiest is probably to create a "proxy server" on your own site. Using PHP or ASP or JSP, you would received the AJAX request from the browser and then the server-side code would go to the foreign site, retrieve the content, and return it back to the AJAX code.
09-27-2012, 01:50 AM
Okay, I've fixed the objects in question. I can't believe I missed something like including the array index. However, it still doesn't seem to work.
I'm assuming that my server is weebly.com, as it hosts my actual website (onemus.weebly.com), so I thought it should be okay. If this isn't correct, how would I create a proxy? Something like document.write()?
Thanks for your responses, by the way.
09-27-2012, 02:06 AM
The same domain policy *DOES* mean the full domain. So, yes, www.weebly.com and onemus.weebly.com are seen as separate domains.
A PHP-based proxy server can be pretty simple.
But it *can* be even simpler than that, if you only want it to always get that same URL.
$url = "http://www.weebly.com/uploads/2/5/1/0/2510225/custom_themes/334455962819258956/files/onemus.xml";
09-27-2012, 02:07 AM
By the by, that was another mistake in your original code: You omitted the "http://" from the URL.
09-27-2012, 03:20 AM
09-27-2012, 03:35 AM
Well, if you put that into a PHP file named, say, "onemus.php" then (assuming your server supports PHP) you could test it by simply putting the URL "http://onemus.weebly.com/onemus.php" and making sure it give you the same XML that you get when you use the full URL
I need to tell you that I don't use PHP. But that code seems dirt simple enough that I can't see why it wouldn't work.
09-27-2012, 03:36 AM
Well, sure. You don't have to use AJAX. I just assumed you wanted to, for some reason.
09-27-2012, 03:48 AM
Either way, it seems to be in vain, as I just found out that Weebly doesn't support php. Are there any other workarounds?
EDIT: I was thinking that maybe I could just post the entire xml file on a page not visible to users, and then access it as normal. This would avoid the same domain issue.
09-27-2012, 05:34 AM
I've gone through with my idea. It seems to be okay with actually accessing the html (with xml nested within), but I'm not sure how to 'catch' the response from the server, because before now, I'd been using synchronous. I believe there's some sort of waiting that needs to happen?
My code as of now:
var xHttp=new XMLHttpRequest();
xHttp.responseType = "document";
09-27-2012, 08:06 PM
Why did you change to async? Just change that true back to false and be happy.
Yes, you can use async, but then you have to set up a listner, etc., etc. If this is supposed to happen before or as the page is displayed, keep it sync for simplicity.
09-27-2012, 08:46 PM
I was following this document (https://developer.mozilla.org/en-US/docs/HTML_in_XMLHttpRequest), which said that I need to use async to correctly receive an html document.
09-27-2012, 09:10 PM
Read that more carefully: It only applies to fetching HTML *AND* PARSING the HTML document *AS* HTML.
If your document is still actually XML, than all that doesn't apply.
Why did you rename to document to ".html"? *IS* it now HTML, instead of XML?
09-27-2012, 09:12 PM
It's now xml embedded within a html page. I figured this was the easiest solution, as Weebly doesn't support php, and there's pretty much no other way that I've found that allows me to host an xml document like that.
EDIT: Apparently a mirror of the file at weebly.com is also hosted at onemus.weebly.com...I'm going to try accessing it once again.
EDIT2: Of course it worked. Thanks for your patience and helping me out here.
Powered by vBulletin® Version 4.2.2 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.