Jump to content

[SOLVED] Firefox getResponseHeader Alternative


sazzie

Recommended Posts

Hi everyone,

I was looking for a way to get my ajax application to responde differently to text and XML in my javascript code.

I found "getResponseHeader" which gets the content-type header. You can then compare against strings to see if its xml or html output, as below :

[code][color=red]
var cType = this.getResponseHeader("Content-Type");
alert("Content-Type : "+cType);
if (cType == "text/xml")
{
                                // act on XML output
}
else if (cType == "text/html")
{
//act on html page
}
else
alert("This Sucks! : "+cType);[/color][/code]

The problem is, "getResponseHeader" doesn't seem to be suported by firefox.

Does anyone know of a work around?
Link to comment
Share on other sites

getResponseHeader is supported in FF.. Just not in the way that you are trying to use it.. It is supposed to be used within an XMLHTTPRequest..

Here is an example that worked fine in FF..

http is my function were I create the XMLHTTPRequest instance..
[code]
function sendRequest() {
http.abort();
http.open('POST', 'includes/process.php?action=getTime');
http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
http.send('do=getTime');
http.onreadystatechange = handleRequest;
    setTimeout('sendRequest()', 1000);
}

function handleRequest() {
if (http.readyState == 4) {
var response = http.responseText;
document.getElementById('time').innerHTML = response;
                //in this case it will alert text/html
alert(http.getResponseHeader('Content-Type'));
}
}
[/code]

I don't think that you can use it out side of a XMLHTTPRequest..


Good Luck,
Tom
Link to comment
Share on other sites

Thank you very much Tom. That works now.

I never noticed I wasn't using the RMLHttpRequest object - it was a piece of code I lifted from the internet
as I was experienting with potential solutions to my problem at the time.

Thanks again.  ;)
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.