我有以下代码,但我无法访问使用 ajax 传递的对象的内容。我错过了什么?
$(".submit").click(function(){
var fruits = {"fruits" : ["apples","banana","oranges"]};
var cars = {"cars" : ["BMW", "Honda", "Toyota"]};
var my_Obj = fruits + cars;
mydata = JSON.stringify(my_Obj);
$("#mydata").val(mydata);
$.ajax({
url: "add.php",
type: "POST",
data: {
name: $('#name').val(),
Address: $('#Address').val(),
my_obj : $mydata
},
datatype: "json",
success: function (status) {
if (status.success == false) {
alert("Failure!");
} else {
alert("Success!");
}
}
});
});
<form id="json" method="post" action="add.php">
<input type="text" name="name" id="name" value="">
<input type="text" name="address" id="Address" value="">
<input type="text" name="mydata" id="mydata" hidden>
<input type="submit" name="submit" id="submit" class="submit" value="send">
</form>
添加.php
$new_post['id'] = $_POST['id'];
$new_post['name'] = $_POST['name'];
$new_post['address'] = $_POST['address'];
$new_post['my_obj'] = $_POST['mydata'];
print_r ($new_post);
[my_obj] 显示 [object Object] [object Object]。谢谢你的帮助。