Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-09-2011, 08:51 PM   PM User | #1
jcobban
New Coder

 
Join Date: Mar 2011
Posts: 12
Thanks: 2
Thanked 0 Times in 0 Posts
jcobban is an unknown quantity at this point
Question Can get XML with XMLHttpRequest but not text

I have been using XMLHttpRequest to retrieve XML documents for some time, but i now find myself needing to retrieve the contents of a normal web page, that is an HTML document, as text. I have been using the sample HTTP code from the O'Reilly Javascript book, fiddling it as necessary.

Code:
/**
 * Use XMLHttpRequest to fetch the contents of the specified URL using
 * an HTTP GET request.  When the response arrives, pass it (as plain
 * text) to the specified callback function.
 * 
 * This function does not block and has no return value.
 *
 * Parameters:
 *	url			URL of the source of the text file
 *	callback	function to call with the returned text document
 *	nfCallback	function to call if the text file is not found
 */
HTTP.getText = function(url, 
			callback, 
			nfCallback) 
{
    var request = HTTP.newRequest();
    request.onreadystatechange = function() {
        if (request.readyState == 4)
	{
	    if (request.status == 200)
        	callback(request.responseText);
	    else
	    if (request.status == 404)
            	nfCallback();
	    else
		alert("request.status: " + request.status);
	}
    }
    request.open("GET", url);
    request.send(null);
};

/**
 * Use XMLHttpRequest to fetch the contents of the specified URL using
 * an HTTP GET request.  When the response arrives, pass it (as a parsed
 * XML Document object) to the specified callback function.
 * 
 * This function does not block and has no return value.
 *
 * Parameters:
 *	url			URL of the source of the XML file
 *	callback	function to call with the returned XML document
 *	nfCallback	function to call if the XML file is not found
 */
HTTP.getXML = function(url,
			callback, 
			nfCallback) 
{
    var request = HTTP.newRequest();
	// define handling of asynchronous response
    request.onreadystatechange = function() {
        if (request.readyState == 4) {
	    if (request.status == 200)
            	callback(request.responseXML);
	    else
	    if (request.status == 404)
            	nfCallback();

	}
    }
    request.open("GET", url);
    request.send(null);
};
Although the two routines appear to be identical except in the type of the parameter passed to the callback routine, the getText routine never invokes the callback because it's onreadystatechange method never gets called with a readyState of 4. The debug alert reports a single invocation with readyState of 0 and the onreadystatechange method is never called again.

I do not have control of the contents of the referenced web page because it is a public web site which does not provide the option of retrieving the information in the form of an XML document.

I am executing the Javascript using Firefox 3.6.15.

In order to solicit the best suggestions for achieving my desired objective you can see the currently incomplete application at http://www.jamescobban.net/database/SubDistForm.php?Census=1901&Province=ON&District=64. In this form the column labelled 'Rel. Frame' is currently empty. The value that needs to be inserted into this column is imbedded in an href on the web page that you can see by clicking on the "Images" button. I would like to automate this so that the web page automatically fills in the required value for the 'Rel. Frame' column by getting the appropriate web page and searching it for the href.
jcobban is offline   Reply With Quote
Old 03-10-2011, 06:47 AM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
Ok ... so from what I understand you were not able to test HTTP.getXML with that page because it "is a public web site which does not provide the option of retrieving the information in the form of an XML document"

From that last statement I presume that your script is violating the Same Origin Policy (SOP). SOP says that you are not generally allowed to make cross-site (cross-domain, cross-protocol) XMLHttpRequests. There has to be a server setting to explicitly allow that from your domain otherwise it just won't work.
devnull69 is offline   Reply With Quote
Reply

Bookmarks

Tags
firefox

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:59 AM.


Advertisement
Log in to turn off these ads.