Preface:
Study-PHP started in the past two days. By the way, I was familiar with JavaScript, HTML, and so on. I found a lot of problems and had to, instead of reading the RFC-doc in E @__@. I am not very good at E-text, headache ing ....
This article is mainly a small attempt, hoping to use a method similar to Ajax, without refreshing post data to the background.
On the network, we can see that the XMLHTTP. setRequestHeader () method needs to be called. Otherwise, the server cannot obtain the post data. --> This is not necessary.
The following describes two methods: You can obtain client-post data on the server without calling setRequestHeader, by the way, it also avoids the trouble of writing URL-encode/decode in JavaScript :)~~~~~~~~~
The main principle is to obtain the data from the client post by obtaining http_raw_post_data.
Client code: (omitted a lot, only write the most important logic)
<Script language = "JavaScript">
// Variable definition -- if you need multiple XMLHTTP-without refreshing the new post, you may need to define a pool-XMLHTTP.
VaR XMLHTTP = NULL;
// Callback method for getting data
Function ajaxget_ OK ()
{
If (XMLHTTP. readystate = 4)
{
Window. Alert ('status = '+ XMLHTTP. status );
Window. Alert ('responsetext = '+ XMLHTTP. responsetext );
}
}
// Main method of post
Function ajaxpost ()
{
// Choose one way of creating the XMLHTTP.
If (window. XMLHttpRequest! = NULL)
{
XMLHTTP = new XMLHttpRequest ();
}
Else if (window. activexobject! = NULL)
{
XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");
}
// Post data here, no use of the [setRequestHeader]-Method
VaR content = "a1 = ABC & b1 = Chinese ####### [#";
XMLHTTP. Open ("Post", "http: // localhost/date/ajaxget. php", true );
XMLHTTP. onreadystatechange = ajaxget_ OK; // see the ajaxget_ OK method.
XMLHTTP. Send (content );
}
</SCRIPT>
Server code: (ajaxget. php)
<? PHP
// Way-1: first set the [always_populate_raw_post_data] to [on] in PHP. ini,
// Then u can get the [http_raw_post_data] Now.
Echo "http_raw_post_data =". $ globals ["http_raw_post_data"];
// Way-2: directly get
// Note this does not work with multipart/form-data because of the way
// File uploads are streamed directly to disk
$ DATA = file_get_contents ('php: // input ');
Echo "Data =". $ data;
?>
The preceding two methods on the server can correctly obtain the hybrid data from the client-post, namely, the daily and E files, without the need for additional encode/decode -- of course, if the post data type is complex, you may need to define your own format (for example, rule/0 differentiation)
Refer:
Http://www.codediesel.com/php/reading-raw-post-data-in-php/
Above all.
Over.
Shadow lei: 2009/06/17-16:59-s.h