So I am setting up a rather simple web form-based database interface using AJAX. I have a Javascript constructor I call [FONT="Courier New"]HttpPipe()[/FONT] [see code for it below]. It has the properties and methods you might expect.
(i) Methods to define parameters for Xml HTTP Request object [FONT="Courier New"].open()[/FONT] method set up the URL, HTTP method (GET|POST|HEAD for now), and whether to be asynchronous.
(ii) The [FONT="Courier New"].send()[/FONT] method gets called, and within that method, the Xml HTTP Request object gets constructed (by function outside [FONT="Courier New"]HttpPipe()[/FONT] constructor)
(iii) the HTTP response handler gets defined as a method in [FONT="Courier New"]HttpPipe()[/FONT]
(1) So I am doing a
[B][FONT="Courier New"]XmlHttpRequest.open("POST", "ajaxRecordsinterface.php", true);[/FONT][/B]
On the server side, I just want to see the raw POST data, and have it sent back to the client to be put into textarea content (which might later serve as a message window to user to wait for server side transaction). But I am not getting the raw POST data sent back. Just the leading text with time/datestamp. The source of the file ajaxRecordsInterface.php for trying to get raw POST data and echo it back as the HTTP response is also below.
(2) In using the dependable Firebug, the Net panel reveals that I am getting a "302 Found" response, which passes on the "Location" header. The FF client was then sending a [FONT="Courier New"]GET[/FONT] request, which I don't want, so I tried to insert handler code that re-packages a 302 response into the wanted method ([FONT="Courier New"]POST[/FONT] request, in this case). The problem is, I have a Firebug breakpoint on the handler entry (see [FONT="Courier New"]this.responseHandler()[/FONT] method), and it is showing a "200 OK" response.
Anyone know why these things do not work as expected?
// Javascript for a constructor--HttpPipe()--for AJAX interface
function HttpPipe() {
this.method = "GET"; // some defaults at construction
this.isAsynchronousRequest = true;
this.mimeType = "text/plain";
this.url = null;
this.requestObject = null;
this.messageWin = null; // even though asynchronous
this.setUrl = function (url) {
if (typeof(url) == "string") {
this.url = url;
return true;
}
return false;
};
this.setMethod = function (methodSet) {
if (typeof(methodSet) == "undefined")
return false;
methodSet = methodSet.toUpperCase();
var approvedMethods = [ "GET", "POST", "HEAD" ];
for (var i = 0; i < approvedMethods.length; i++)
if (approvedMethods[i] == methodSet) {
this.method = methodSet;
return true;
}
return false;
};
this.setMimeType = function (mimeType) {
if (typeof(mimeType) == "undefined")
return false;
var approvedMimeTypes = [ "text/xml", "text/plain", "text/html" ];
for (var i = 0; i < approvedMethods.length; i++)
if (approvedMimeTypes[i] == mimeType) {
this.mimeType = mimeType;
return true;
}
return false;
};
var httpPipe = this; // for being able to put response handler in object
this.sendData = function (formData, handler) {
this.formData = formData; // for non-200 responses!
this.requestObject = XmlHttpRequestObject(); // defined in 'ajax.js' to find right constructor
if (this.requestObject.overrideMimeType)
this.requestObject.overrideMimeType(this.mimeType);
this.requestObject.onreadystatechange = function () {
httpPipe.responseHandler(httpPipe.requestObject);
};
// this.requestObject.open("POST", this.url, this.isAsynchronousRequest); // debugging code
this.requestObject.open(this.method, this.url, this.isAsynchronousRequest); // to-be-final code
this.requestObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
this.requestObject.setRequestHeader("Content-Length", this.formData.length);
this.requestObject.setRequestHeader("Connection", "close"); // not sure about need for this ?
this.messageWin = new userWaitMessage(); // may not need this in final version
this.messageWin.open();
if (this.method == "GET")
this.requestObject.send(null);
else if (this.method == "POST")
this.requestObject.send(formData);
};
this.responseHandler = function (reqObj) {
if (reqObj.status == 302) { // Found response
// re-package the data and use Location header for URL
reqObj.open(httpPipe.method, reqObj.getResponseHeader("Location"),
httpPipe.isAsynchronousRequest);
reqObj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
reqObj.setRequestHeader("Content-Length", httpPipe.formData.length);
reqObj.setRequestHeader("Connection", "close");
reqObj.send(httpPipe.formData);
} else if (reqObj.readyState == COMPLETE) {
for (var i = 0; i < httpResponseCodes.length; i++)
if (reqObj.status == httpResponseCodes[i].code)
break;
if (i == httpResponseCodes.length) {
alertUnknownHttpResponse();
alert("Unknown HTTP Response! Code = " + reqObj.status);
}
httpPipe.messageWin.close();
// most any valid HTTP response should be placed in the message window
var msgPlace = httpPipe.messageWin.messageWin.document.getElementById("ajaxResponse");
msgPlace.value = reqObj.responseText;
}
};
}
<?php
// PHP file "ajaxRecordsInterface.php"
/* header() and output buffering calls pulled off some other
forum post...willing to try anything at this point */
header("Cache-Control: no-cache, must-revalidate");
header("Content-Type: text/plain");
header("Connection: close");
ob_start();
//var_dump(headers_list());
echo "Here is what you POSTed at ".date("D j M Y H:i:s", time()).":";
echo file_get_contents("php://input");
// in that forum post, readfile() was used, but was ineffective
header("Content-length: ".ob_get_length());
ob_end_flush();
flush();
exit();
?>