Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP POST variable not defined when using Chrome + XHR
    primarykey
    data
    text
    <p>This question is nearly identical to the year-old unanswered question: </p> <p><a href="https://stackoverflow.com/questions/8033263/http-post-xhr-not-working-in-chrome">HTTP-POST XHR not working in chrome</a>.</p> <p>I was hoping to find a solution and can offer more details than the original question.</p> <p><strong>Background</strong></p> <p>I have a sever-side php script that is supposed to take POST data from a form or XHR request. When I test the site using a form for submission, it works in Chrome and IE9. However, when I generate the request using XHR, the php POST variable is undefined when using Chrome on the client side. This behavior is inconsistent: about 1 in 20 tries, the php does accept the data.</p> <p>I have checked the <code>php://input</code> stream and see that the post data is being sent to server in all cases; and note that a small few of the HTTP headers (<code>$_SERVER</code>) are different between test cases.</p> <p><strong>Code</strong></p> <p>Server side:</p> <pre><code>&lt;?php echo file_get_contents("php://input") . "\n"; print_r($_SERVER); print_r($_POST); ?&gt; </code></pre> <p>Client side form version (Chrome and IE9 both work)</p> <pre><code>&lt;form action="scriptName.php" method="post"&gt; Field: &lt;input type="text" name="myField"&gt;&lt;br&gt; &lt;input type="submit" value="Submit"&gt; &lt;/form&gt; </code></pre> <p>Client side XHR version (IE9 always works, Chrome works about 5% of time)</p> <pre><code>&lt;script&gt; function postToURL(url,data) { // Typical XHLHttpRequest declarations are removed for brevity // - checks browser type and declares xmlhttp accordingly // - defines a onreadystatechange handle xmlhttp.open("POST",url,false); xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xmlhttp.send(data); } &lt;/script&gt; ... &lt;form&gt; &lt;button type="button" onClick="postToURL('scriptname.php','myField=test');"&gt; Test &lt;/button&gt; &lt;/form&gt; </code></pre> <p><strong>Output</strong></p> <p>In all cases the request body data (<code>php://input</code>) returns the same value. The headers are mostly the same, but Chrome adds an <code>[HTTP_PRAGMA] =&gt; no-cache</code> in XHR mode</p> <p>The <code>$_POST</code> variable is defined as <code>[myField] =&gt; test</code> except in the Chrome + XHR case.</p> <p><strong>Questions</strong></p> <p>Where is the problem likely occurring? Is there something wrong with the HTTP headers possibly, or should I be looking server-side. Any ideas?</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.
 

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