Note that there are some explanatory texts on larger screens.

plurals
  1. POAjax is correctly returning and displaying data but error message is being activated
    primarykey
    data
    text
    <p>I'm performing AJAX using the following code:</p> <pre><code>function main() { // get the name fields var name1 = document.getElementById("name1").value; var name2 = document.getElementById("name2").value; // Encode the user's input as query parameters in a URL var url = "response.php" + "?name1=" + encodeURIComponent(name1) + "&amp;name2=" + encodeURIComponent(name2); // Fetch the contents of that URL using the XMLHttpRequest object var req = createXMLHttpRequestObject(); req.open("GET", url); req.onreadystatechange = function () { if (req.readyState == 4 &amp;&amp; req.status == 200) { try { // If we get here, we got a complete valid HTTP response var response = req.responseText; // HTTP response as a string var text = JSON.parse(response); // Parse it to a JS array // Convert the array of text objects to a string of HTML var list = ""; for (var i = 0; i &lt; text.length; i++) { list += "&lt;li&gt;&lt;p&gt;" + text[i].reply + " " + text[i].name + "&lt;/p&gt;"; } // Display the HTML in the element from above. var ad = document.getElementById("responseText"); ad.innerHTML = "&lt;ul&gt;" + list + "&lt;/ul&gt;"; } catch (e) { // display error message alert("Error reading the response: " + e.toString()); } } else { // display status message alert("There was a problem retrieving the data:\n" + req.statusText); } } req.send(null); } // creates an XMLHttpRequest instance function createXMLHttpRequestObject() { // xmlHttp will store the reference to the XMLHttpRequest object var xmlHttp; // try to instantiate the native XMLHttpRequest object try { // create an XMLHttpRequest object xmlHttp = new XMLHttpRequest(); } catch (e) { // assume IE6 or older try { xmlHttp = new ActiveXObject("Microsoft.XMLHttp"); } catch (e) {} } // return the created object or display an error message if (!xmlHttp) alert("Error creating the XMLHttpRequest object."); else return xmlHttp; } </code></pre> <p>This works exactly as planned, the code within the try block is executed perfectly. But the alert <em>"There was a problem retrieving the data:</em> is also activated, with <code>req.statusText</code> displaying "OK".</p> <p>How can this be possible? How can the code within the if statement activate perfectly but at the same time the else block is activated?</p> <p>I'm stumped, any ideas?</p> <p>The servor code is simply:</p> <pre><code>&lt;?php if( $_GET["name1"] || $_GET["name2"] ) { $data = array( array('name' =&gt; $_GET["name1"], 'reply' =&gt; 'hello'), array('name' =&gt; $_GET["name2"], 'reply' =&gt; 'bye'), ); echo json_encode($data); } ?&gt; </code></pre> <p>And the HTML:</p> <pre><code>&lt;input id="name1"&gt; &lt;input id="name2"&gt; &lt;div id="responseText"&gt;&lt;/div&gt; &lt;button onclick="main();"&gt;Do Ajax!&lt;/button&gt; </code></pre>
    singulars
    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