Jump to content

Problem using setRequestHeader


pixelbrain

Recommended Posts

I am trying to change my http Accept header to GET content I want from my server. I have been doing this with the setRequestHeader method. So far it does work, but only in IE using Apache 2 as a server. I have tried all combinations of IIS, Apache, Firefox, IE, and Opera. Is there something I am missing? I am happy serving with Apache, but I would like the code to work in all modern browsers.This is index.html:

<html><head><title>Ajax test</title><script>function request(library){    var url = "/libraries/" + library;   var req;  // The request object      try{      // Opera 8.0+, Firefox, Safari      req = new XMLHttpRequest();   } catch (e){      // Internet Explorer Browsers      try{         req = new ActiveXObject("Msxml2.XMLHTTP");      } catch (e) {         try{            req = new ActiveXObject("Microsoft.XMLHTTP");         } catch (e){            // Something went wrong            alert("Sorry, this page is currently not functioning or does not support your browser. Please try again later.");            return false;         }      }   }       req.open( 'get', url, true );    req.onreadystatechange = function () { callback(req); } ;    // setRequestHeader can only be called while in readystate 1    // We have to set the qvalue to 1.1 because Konqueror sends    // it's standard Accept header with our header tacked on the end    // which means that text/html gets picked first.    // According to the w3c spec released in June 06, this method should OVERWRITE the Accept header    req.setRequestHeader('Accept','text/xml; q=1.1');    // Above line only works in IE with Apache 2 for me!    req.send(null); //modified per keelypavan suggestion}function callback(req){    if (req.readyState == 4){        alert(req.getAllResponseHeaders());        if (req.status == 200){            parseMessage(req);        }    }}function parseMessage(req){    var message = req.responseXML;    var root_node = message.getElementsByTagName('name').item(0);    alert(root_node.firstChild.data);}</script></head><body></body></html>

If you are interested in the server code to handle the request, although it is not important, here it is:

<?php$xmlData = <<< XMLDATA<xml><test>    <name>working!!</name></test>XMLDATA;function printHttpHeader($mime){    header("Pragma: public");    header("Cache-Control: no-cache");    header('Content-Type: '.$mime);}/** * switch based on the header of the current request. This header was fetched * from the server and contents were put into these $_SERVER variables */if($_SERVER["REQUEST_METHOD"] == "GET"){    if(strstr($_SERVER["HTTP_ACCEPT"], "text/html")){        $mime = "text/html";        printHttpHeader($mime);        print $_SERVER["HTTP_ACCEPT"];        exit;    }    //the goal is to change the request header so that only text/xml is the only value for Accept    else if(strstr($_SERVER["HTTP_ACCEPT"], "text/xml")){        $mime = "text/xml"; //makes responseXML data available to the javascript        printHttpHeader($mime);        print $xmlData;        exit;    }   }?> If you have any other questions, or any ideas, let me know. Thanks!

Link to comment
Share on other sites

what do you mean? If you dont change it, it will stay as get since thats the default way to do it..
Are you saying that the first parameter of open() is optional, since by default it uses GET? So would it be something like req.open( url, true ); ? I intend to have seperate methods for POST, GET, and DELETE from the same URL. I am just trying to get GET working right now. My code does what I want in IE. Firefox and Opera are not playing nicely though. :-P
Link to comment
Share on other sites

okay, you should keep it get. But the mime is automatically get, so no need to change anything.. but if you want to use post you will have to reset the mime. Heres my ajax request making code:

r = 0;request = new Array();reqChanges = new Array();response = new Array();function Ajax(a, url, mode, data) {var requester = null;reqChanges[a] = 0;try { requester = new XMLHttpRequest(); }catch (error) { try { requester = new ActiveXObject("Microsoft.XMLHTTP"); } catch (error) {  try { requester = new ActiveXObject("Msxml2.XMLHTTP"); }  catch (error) {   alert("Failed to Initiate Request");   return false;  } }}requester.open(mode, url);if (mode == "POST") { requester.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); requester.setRequestHeader("Content-length", data.length); requester.setRequestHeader("Connection", "close");}requester.send(data);return requester;}function AjaxHandle(requester, a) {reqChanges[a]++;// If XMLHR object has finished retrieving the dataif (requester.readyState == 4){ try {  if (requester.status == 200) {   response[a] = requester.responseText;   return true;  } else if (requester.status != 0) { alert("Error retrieveing URL: "+requester.status); return false; } } catch (error) { alert(error); return false; } return false;} else { return false; }}

Just in case you want to use it.. just put that in a seperate file and include it.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...