I would like to send data using the jQuery Ajax API:
var myData = {"param1" : $('#txtParam1').val(), "param2" : $('#txtParam2').val()};
$.ajax({
url: 'DataService.php?action=SomeAction',
type: 'POST',
data: myData,
dataType: 'json',
contentType: "application/json; charset=utf-8",
success: function(result) {
alert(result.Result);}
});
When I tried to retrieve this data with PHP using
$param1 = $_REQUEST['param1'];
$param1
is showing null
and print_r($_REQUEST)
is only showing action = SomeAction ..
How do I retrieve the posted data on a PHP page?
since you're sending the ajax as "contentType: 'application/json'", you need to fetch the request body with php://input like so:
$request = file_get_contents("php://input"); // gets the raw data
$params = json_decode($request,true); // true for return as array
print_r($params);
Try this:
$params = json_decode( $_POST['param1']);
And then check what you have got:
var_export( $params);
Or you can use a foreach
loop:
foreach( $params as $param)
{
echo $param . '<br />';
}
You are using POST, method, dont use REQUEST because it is also less secure.
尝试使用
$param1 = $_POST['param1'];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.