Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Mar 2011
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    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.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •