Note that there are some explanatory texts on larger screens.

plurals
  1. POAJAX POST Request Only Works Once in Safari 5
    primarykey
    data
    text
    <p>I use my own custom AJAX library (I'm not interested in using jQuery, etc.), which is working flawlessly in the following browsers:</p> <ul> <li>Firefox 7</li> <li>Chrome 14</li> <li>IE 8</li> <li>IE 8 (compatibility mode)</li> </ul> <p>Using my custom AJAX library in the aforementioned browsers, I can make as many AJAX requests as I want, in any order, using GET and/or POST methods, and they all work flawlessly. Since a new AJAX object is created for every request (see code below), I can even have more than one AJAX request process simultaneously with success.</p> <p>However, in Safari 5 an AJAX POST request only passes POST data to the server if it is the absolute first AJAX request to execute. Even if I execute the exact same AJAX POST request twice in a row, the POST data is only passed to the server during the first request. Here is the JavaScript in my custom AJAX library:</p> <pre><code>if (!Array.indexOf) { Array.prototype.indexOf = function(obj) { for (var i = 0; i &lt; this.length; i++) { if (this[i] == obj) { return i; } } return -1; }; } function ajaxObject() { if (window.ActiveXObject) { var activexmodes = ["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"]; for (var i = 0; i &lt; activexmodes.length; i++) { try { return new ActiveXObject(activexmodes[i]); } catch (e) { } } } else if (window.XMLHttpRequest) { return new XMLHttpRequest(); } else { return false; } } function ajaxRequest(aURI, aContainerId, aPostData, aResponseType, aAvoidBrowserCache) { // Initialize var xmlhttp = new ajaxObject(); xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 &amp;&amp; xmlhttp.status == 200) { if (aResponseType != "eval" &amp;&amp; aResponseType != "EVAL") { // Show HTML for response document.getElementById(aContainerId).innerHTML = xmlhttp.responseText; } else { // Parse &amp; execute JavaScript for response var responseText = xmlhttp.responseText; var startPos, endPos; for (var i = 0; i &lt; responseText.length; i++) { if (responseText.substring(i, i + 6) == "&lt;eval&gt;") { startPos = i + 6; break; } } for (var i = startPos; i &lt; responseText.length; i++) { if (responseText.substring(i, i + 7) == "&lt;/eval&gt;") { endPos = i; break; } } textToEval = responseText.substring(startPos, endPos); eval(textToEval); } } else { try { if (xmlhttp.status != 0 &amp;&amp; xmlhttp.status != 200) { alert('Error ' + xmlhttp.status); } } catch (e) { // Handle IE8 debug "unknown error" } } } if (aAvoidBrowserCache != false) { // Combat browser caching: aURI = aURI + (aURI.indexOf("?") == -1 ? "?" : "&amp;"); theTime = new Date().getTime(); aURI = aURI + theTime + "=" + theTime; } // Make request if (typeof aPostData == "undefined" || aPostData == null || aPostData == "") { // GET request xmlhttp.open("GET", aURI, true); xmlhttp.send(); } else { // POST request var parameters = ""; if (aPostData.constructor.toString().indexOf("Array") != -1) { // Use parameters passed as array for (var postCount = 0; postCount &lt; aPostData.length; postCount++) { if (parameters != "") { parameters = parameters + "&amp;"; } parameters = parameters + aPostData[postCount][0] + "=" + encodeURIComponent(aPostData[postCount][1]); } } else { // Use parameters passed as string parameters = aPostData; } xmlhttp.open("POST", aURI, true); xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xmlhttp.send(parameters); } } </code></pre> <p>So for example, either of the following AJAX POST requests will pass POST data if they are the absolute first AJAX request (whether GET or POST); otherwise, the POST data is not passed:</p> <pre><code>ajaxRequest("test.aspx", "", [["name1","value1"],["name2","value2"]], "eval"); </code></pre> <p>or</p> <pre><code>ajaxRequest("test.aspx", "", "name1=value1&amp;name2=value2", "eval"); </code></pre> <p>I have added debug statements all throughout my AJAX library, and the POST parameters are being created in the "parameters" variable as expected prior to each POST request. I have absolutely no idea why, only in Safari 5 (out of the mentioned browsers), I have this problem. Any ideas?</p> <p>Thanks in advance! Jesse</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload