Re: Parsing AJAX post data -- The Way
| From: | M5 | Date: | Fri, 26 Jan 2007 05:02:31 +0000 |
| Subject: | Re: Parsing AJAX post data -- The Way | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-247806@lists.php.net to get a copy of this message | ||
On 25-Jan-07, at 4:46 PM, Richard Lynch wrote:
On Thu, January 25, 2007 12:41 am, M5 wrote:You're right in that "*IF* AJAX is sending POST data correctly" everything is okay--that is, will $_POST contain the posted data as array elements.Just wondering what smart people do for parsing data sent by the Javascript XMLHTTP object--e.g., http.send("post",url,true)... In a normal form submit, the $_POST global nicely allocates form elements as array elements automatically. But with the AJAX way, the data get stuffed inside $HTTP_RAW_POST_DATA as a string, thereby making extraction more tedious.Call me crazy, but if AJAX is sending POST data correctly, your PHP code shouldn't have to do anything special...
POST data is POST data. The $HTTP_RAW_POST_DATA should be there as well, if you turned that on, but that doesn't make $_POST go away.Actually, that's not true. If the POST data is not set with the correct headers... http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); http.setRequestHeader("Content-length", payload.length); http.setRequestHeader("Connection", "close"); ...then $_POST will be empty and the data that is sent can only be accessed from $HTTP_RAW_POST_DATA (which incidentally is off by default). That was my problem--I wasn't sending those http headers. An earlier poster pointed it out to me, and that solved the problem. ...Rene