Jump to content

pixelbrain

Members
  • Posts

    4
  • Joined

  • Last visited

Everything posted by pixelbrain

  1. 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
  2. Is there any way I can keep it as GET? I am trying to build a REST web service, so I want GET, POST, and DELETE to do different things. Thanks.
  3. 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!
  4. Please Delete this post moderators, i changed the title in a post above since i couldn't do it in this one.
×
×
  • Create New...