Jump to content

[SOLVED] help with XMLHttpRequest()


bennyboywonder

Recommended Posts

Currently I am experementing with ajaxing my blog. I am learning to use the XMLHttpRequest object, but am finding it a little sporadic. It appears to work and not work sporadically. For example the below function works is fired up as soon as the page loads, writing all the entries onto the main DIV. However, if I create a new function by litterally copying and pasting, and then have this function fired by a form button, the code in the onreadystatechange function, doesn't seem to fire anymore. Can anyone else help me with this. It seems rather arbitrary.

function loadPosts(month, year, entry) {
toggleLayer('mainloadingbox');
var querystring = "action=readblog";
if(entry) querystring += "&entry=" + entry;
else if (month && year) querystring += "&month=" + month + "&year=" + year;
var url = "scripts/action.php?" + querystring;
var xhReq = createXMLHttpRequest();
xhReq.open("GET", url, true);
xhReq.onreadystatechange = function() {
	if (xhReq.readyState != 4)  { return; }
	if (xhReq.status != 200) { return; }
	var xml = xhReq.responseXML.getElementsByTagName("post");
	toggleLayer('mainloadingbox');
	for (var i = 0; i < xml.length; i++)
	{
		titleNode = xml[i].getElementsByTagName("title");
		posttitle = "<h1>" + titleNode[0].firstChild.data + "</h1>";
		bodyNode = xml[i].getElementsByTagName("body");
		body = "<div>" + bodyNode[0].firstChild.data + "</div>";
		with(document.getElementById("main")) {
			innerHTML += posttitle;
			innerHTML += body;
		}
	}
}
xhReq.send(null);
}

Link to comment
Share on other sites

Well... First off I normally create a separate function for dealing with the xmlhttprequest. This will make it easier and reusable. Here is a simple cross browser example of what I mean.

 

function createRequestObject() {
    if (window.XMLHttpRequest) { // Mozilla, Safari, Opera...
        var xmlhttp = new XMLHttpRequest();
        if (xmlhttp.overrideMimeType)
    xmlhttp.overrideMimeType('text/xml');
    }
else if (window.ActiveXObject) { // IE
        try {
            var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                  var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }

    if (!xmlhttp) {
        alert('Giving up  Cannot create an XMLHTTP instance');
        return false;
    }
return xmlhttp;
}

var XhReq = createRequestObject();

 

If you are not dealing with xml then you will want to comment these two lines or you will get an error in FF

if (xmlhttp.overrideMimeType)
     xmlhttp.overrideMimeType('text/xml');

 

i think that if you do it like this you will find that it is more stable.

 

Tom

Link to comment
Share on other sites

Well... First off I normally create a separate function for dealing with the xmlhttprequest. This will make it easier and reusable. Here is a simple cross browser example of what I mean.

 

function createRequestObject() {
    if (window.XMLHttpRequest) { // Mozilla, Safari, Opera...
        var xmlhttp = new XMLHttpRequest();
        if (xmlhttp.overrideMimeType)
    xmlhttp.overrideMimeType('text/xml');
    }
else if (window.ActiveXObject) { // IE
        try {
            var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                  var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }

    if (!xmlhttp) {
        alert('Giving up  Cannot create an XMLHTTP instance');
        return false;
    }
return xmlhttp;
}

var XhReq = createRequestObject();

 

If you are not dealing with xml then you will want to comment these two lines or you will get an error in FF

if (xmlhttp.overrideMimeType)
     xmlhttp.overrideMimeType('text/xml');

 

i think that if you do it like this you will find that it is more stable.

 

Tom

 

Thanks, but I already had one, that's what createXMLHttpRequest() was. That isn't the problem...

function createXMLHttpRequest() {
   try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
   try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
   try { return new XMLHttpRequest(); } catch(e) {}
   alert("XMLHttpRequest not supported");
   return null;
}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.