Javascript 无法从服务器端提取阵列
我想从php脚本中获取一些数据到我的html页面。他们的数组$UniqueNames在服务器端有一个值,但是当我在html页面上使用json_encode时,似乎什么都没有发生,console.log返回一个空数组BilderID。有什么建议吗 代码: Php:Javascript 无法从服务器端提取阵列,javascript,php,arrays,ajax,Javascript,Php,Arrays,Ajax,我想从php脚本中获取一些数据到我的html页面。他们的数组$UniqueNames在服务器端有一个值,但是当我在html页面上使用json_encode时,似乎什么都没有发生,console.log返回一个空数组BilderID。有什么建议吗 代码: Php: 解决方案是删除数据类型说明符,在php中回显数组,并在success方法中接收它: $.ajax({ url: 'Includes/Bildhantering.php', // point to server
解决方案是删除数据类型说明符,在php中回显数组,并在success方法中接收它:
$.ajax({
url: 'Includes/Bildhantering.php', // point to server-side PHP script
//dataType: 'text', // what to expect back from the PHP script
cache: false,
contentType: false,
processData: false,
data: form_data,
type: 'post',
success: function (response) {
BilderID = response;
console.log(BilderID);
},
error: function (response) {
console.log("error:");
}
});
我的意思是,如果javascript能够解决这个问题,为什么还要使用数据类型呢?数据类型应该设置为json,而不是jsontext@treyBake还是没什么。他没有从PHP文件中输出任何JSON…你把服务器端和前端/DOM混合在一起,它不会像你设置的那样工作。哦,等等。。。为什么要在jquery脚本中回显PHP变量?使用响应。。
$UniqueNames = array();
for($i=0;$i<count($file_array);$i++)
{
if($file_array[$i]['error']){
echo $phpFileUploadErrors[$file_array[$i]['error']];
} else {
$extensions = array('jpg','png','gif','jpeg');
$file_ext = explode('.',$file_array[$i]['name']);
$file_ext = end($file_ext);
if (!in_array($file_ext, $extensions)){
echo "Invalid file extension!";
} else {
$fileNameNew = uniqid('', true).".".$file_ext;
$UniqueNames[] = $fileNameNew;
move_uploaded_file($file_array[$i]['tmp_name'], 'Bilder/'.$fileNameNew);
echo $phpFileUploadErrors[$file_array[$i]['error']];
}
}
}
$.ajax({
url: 'Includes/Bildhantering.php', // point to server-side PHP script
//dataType: 'text', // what to expect back from the PHP script
cache: false,
contentType: false,
processData: false,
data: form_data,
type: 'post',
success: function (response) {
BilderID = response;
console.log(BilderID);
},
error: function (response) {
console.log("error:");
}
});