After performing a GET request on a URL that definitely exists, I attempted to open an XMLHttpRequest using the POST method. However, when I called the send() function on the XHR object, no connection was established with the server and no data was sent. Even after confirming this using WireShark, all I got was an error flag with a status of 0 in the onreadystatechange event handler.
The strange part is that there seems to be no way for me to diagnose what exactly went wrong. Despite analyzing the headers and body in FireBug, everything appeared fine without any response body or header present. The only indication of an issue was a red circle with an X during the POST /url (x) console output, with no visible error message provided. This particular line did not even show up in the NET panel, which adds to the mystery surrounding this problem.
function save_edits(url, txt, cb)
{
var xhr = new XMLHttpRequest();
console.log("'POST' " + url);
xhr.open('POST', url, true);
xhr.onreadystatechange = function(ev)
{
if (xhr.readyState == 4)
{
console.log('post complete status:' + xhr.status);
cb();
}
}
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
var text = "&" + edit_what + "=" + escape(txt.replace(/\r/g, '\n'));
try
{
xhr.send(text);
}
catch (e)
{
console.log('send() error: ' + e.toString());
}
Upon viewing the FireBug console, the following was displayed:
- 'POST'
- POST source.js (line 70)
- post complete status:0
Line 1) contains my initial console log statement.
Line 2) signifies the failure of the POST XHR request, denoted by the red circle-X symbol.
Line 3) corresponds to the console log message triggered by the onreadystatechange handler.
Even with added checks for exceptions and switching to synchronous behavior, no errors were thrown. It appears that the issue lies not on the server side, as the request never actually reaches the server, leaving me puzzled about how to pinpoint the root cause.