im trying to pass an array of data to a PHP script using Jquery's AJAX; this is my JS:
var dataObject = {
shippingDataRow: [{
name: $('#shippingData_name').val(),
street: $('#shippingData_street').val(),
number: $('#shippingData_number').val(),
}]
};
$.ajax({
type: "post",
data: { action: 'test', data: dataObject },
cache: false,
url: "php/post_test.php",
});
And this is the PHP (just doing some tests, before I can write all procedures),
<?php
function test(){
$dataObject = $_POST['dataObject'];
print_r( $dataObject );
}
?>
In Chrome's Developer Tools, I can see the Status Code:200 OK; even all the array with the correct values, but when I open the php it doesn't work.
Any ideas?
try: $dataObject = $_POST['data'];
You need to refer to the key of the posted data, not the value in javascript.
Change:
$dataObject = $_POST['dataObject'];
To:
$dataObject = $_POST['data'];
您的帖子应该是
$_POST['data'];
您没有在任何地方调用test()。so,它的提示返回200ok但响应为null。在test_php中回显任何内容并检查。
var dataObject = {
shippingDataRow: {
name: $('#shippingData_name').val(),
street: $('#shippingData_street').val(),
number: $('#shippingData_number').val()
}
};
$.ajax({
type: "post",
data: { action: 'test', data: dataObject },
cache: false,
url: "php/post_test.php"
});
-
<?php
$dataObject = $_POST['data'];
print_r( $dataObject );
?>
As others mentioned, $_POST['data'] will get you the data array.
Also, using $_REQUEST works well as it'll target both get/post requests.
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.