所以我有2个php文件,我们称之为UploadImages.php和Caller.php
所以在UploadImages.php中我有一个像这样的函数
<?php
function UploadImages(){
$result = [
"UploadOk" => 0,
"UploadMsg" => "Upload successful" ];
echo(json_encode($result));
}
?>
这就是我在Caller.php中处理它的方式
<?php
include "UploadImages.php";
$uploadResult = json_decode(UploadImages(), true);
if($uploadResult["UploadOk"] == 1) {
// do something else
}
else {
echo $uploadResult["UploadMsg"];
}
?>
使用javascript的ajax,我得到了这个结果:
"{"UploadOk":1,"UploadMsg":"Upload successful"}"
我所期望的只是UploadMsg属性而不是它返回一个对象,请注意我实际上需要处理Caller.php中的UploadOk而不是仅仅将整个对象转储到javascript,所以javascript中的JSON.parse方法不会是处理这种情况的正确方法。
答案 0 :(得分:0)
echo
内的 UploadImages
返回对象,因为PHP echo
发送响应头,因此通过ajax调用,您将获得此方法的响应。
使用以下方法更改方法,然后尝试:
function UploadImages(){
$result = [
"UploadOk" => 0,
"UploadMsg" => "Upload successful" ];
return json_encode($result);
}
注意:您也不需要在json_encode
内使用UploadImages
,只需按以下方式返回数组:
function UploadImages(){
$result = [
"UploadOk" => 0,
"UploadMsg" => "Upload successful" ];
return $result;
}
和Caller.php
<?php
include "UploadImages.php";
$uploadResult = UploadImages();
if($uploadResult["UploadOk"] == 1) {
// do something else
}
else {
echo $uploadResult["UploadMsg"];
}
?>