使用Ajax发布PHP对象数组
大家好,我似乎不知道我做错了什么,我有一个文件的对象数组,并试图通过该数组被另一个使用ajax的php文件处理 我的php对象数组是使用Ajax发布PHP对象数组,php,arrays,json,ajax,Php,Arrays,Json,Ajax,大家好,我似乎不知道我做错了什么,我有一个文件的对象数组,并试图通过该数组被另一个使用ajax的php文件处理 我的php对象数组是 Array ( [0] => Files Object ( [id] => 39 [case_id] => 35 [file_name] => 5M UK Limited - Annual progress report 2017.pdf
Array
(
[0] => Files Object
(
[id] => 39
[case_id] => 35
[file_name] => 5M UK Limited - Annual progress report 2017.pdf
[file_path] => 5MUK01C/5M UK Limited - Annual progress report 2017.pdf
[file_size] => 5233880
[file_type] => application/pdf
[file_date] => 2017-04-25 10:28:28
[downloads] => 11
[temp_path:Files:private] =>
[upload_dir:protected] => /Users/johnfieldsend/Library/Mobile Documents/com~apple~CloudDocs/Web Development/document.center.new/httpdocs/uploads
[upload_errors:protected] => Array
(
[0] => No Errors.
[1] => Larger then upload_max_filesize - Try to reduce the sizre of your file.
[2] => Larger than the MAX_FILE_SIZE - Try to reduce the sizre of your file.
[3] => Partial upload.
[4] => No file.
[6] => No template directory.
[7] => Can't write to disk
[8] => File type not allowed, please upload either a word or pdf file.
)
[errors] => Array
(
)
)
[1] => Files Object
(
[id] => 40
[case_id] => 35
[file_name] => Notice to opt out 5M UK Limited.pdf
[file_path] => 5MUK01C/Notice to opt out 5M UK Limited.pdf
[file_size] => 182099
[file_type] => application/pdf
[file_date] => 2017-04-25 10:32:54
[downloads] => 4
[temp_path:Files:private] =>
[upload_dir:protected] => /Users/johnfieldsend/Library/Mobile Documents/com~apple~CloudDocs/Web Development/document.center.new/httpdocs/uploads
[upload_errors:protected] => Array
(
[0] => No Errors.
[1] => Larger then upload_max_filesize - Try to reduce the sizre of your file.
[2] => Larger than the MAX_FILE_SIZE - Try to reduce the sizre of your file.
[3] => Partial upload.
[4] => No file.
[6] => No template directory.
[7] => Can't write to disk
[8] => File type not allowed, please upload either a word or pdf file.
)
[errors] => Array
(
)
)
)
此数组存储在变量$files
我正试图用下面的代码通过ajax将其传递给我的charts.php
文件
var jsonData = $.ajax({
type: 'POST',
url: '/ajax/process/charts.php?chart=files',
data: <?php echo json_encode($files, true) ?>,
dataType: "json",
async: false
}).responseText;
但是我无法在charts.php中访问这些数据
我的回答是
array(1) {
["undefined"]=>
string(0) ""
}
问题可能如下所示:
var jsonData = $.ajax({
type: 'POST',
url: '/ajax/process/charts.php?chart=files',
data: {
"myArray": '<?php echo json_encode($files) ?>'
}
dataType: "json",
async: false
}).responseText;
可能AJAX调用中的数据定义不正确,请尝试以下操作:
var jsonData = $.ajax({
type: 'POST',
url: '/ajax/process/charts.php?chart=files',
data: {
"myArray": '<?php echo json_encode($files) ?>'
}
dataType: "json",
async: false
}).responseText;
两天前我遇到了类似的问题,但我的解决方案不同,我发送的阵列如下:
data { "myArray": '<?php echo urlencode(json_encode($myArray));?>' }
json_decode(urldecode($_POST['myArray']));
希望对您有所帮助因为您没有与我们共享charts.php代码,所以很难排除故障。最好的工具是使用浏览器的开发人员工具,特别是查看“网络”,然后选择“XHR”子选项卡。触发您的AJAX调用,看看发生了什么-它是否以200/成功的响应发布?如果是,则“响应”子选项卡中的信息是什么?“请求”子选项卡中的信息是什么?使用该工具,您可以添加类似var\u dump($\u POST)的内容
到您的charts.php代码,并开始查看在启动AJAX调用时“response”子选项卡中发生的情况。感谢Alen,是的,它只是向我的数组添加了一个标识符,然后json_解码该post字段
json_decode(urldecode($_POST['myArray']));