大家好我无法弄清楚我做错了什么,我有一个文件的对象数组并试图通过另一个php文件使用ajax来传递该数组
我的php对象数组是
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;
如果我检查页面,我可以看到我的数组的json字符串被传递给ajax
type: 'POST',
url: '/ajax/process/charts.php?chart=files',
data: [{"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","errors":[]},{"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","errors":[]}],
dataType: "json",
async: false
但我无法访问charts.php
我的回复是
array(1) {
["undefined"]=>
string(0) ""
}
答案 0 :(得分:1)
问题可能如下:
也许在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;
并尝试使用(在charts.php中)回显它:
echo json_decode($_POST['myArray']);
我在2天前遇到了类似的问题,但我的解决方案有所不同,我发送了这样的数组:
data { "myArray": '<?php echo urlencode(json_encode($myArray));?>' }
然后像这样抓住它:
json_decode(urldecode($_POST['myArray']));
希望有所帮助