Javascript 如何在php函数中返回数组
我使用Laravel5.4和干预插件将图像上传为ajax 我将在php控制器上传图像,它将返回一个响应(文件名) php返回的变量是一个数组,但在javascript中它是字符串,我无法迭代Javascript 如何在php函数中返回数组,javascript,php,ajax,laravel,intervention,Javascript,Php,Ajax,Laravel,Intervention,我使用Laravel5.4和干预插件将图像上传为ajax 我将在php控制器上传图像,它将返回一个响应(文件名) php返回的变量是一个数组,但在javascript中它是字符串,我无法迭代 public function upload(Request $request) { $array = $request->file('image'); $count = count($array); for ($i=0 ; $i<$count; $i++)
public function upload(Request $request)
{
$array = $request->file('image');
$count = count($array);
for ($i=0 ; $i<$count; $i++)
{
$img = Image::make($request->file('image')[$i]);
$img->widen(800);
$img->fit(800,600);
$rnd = rand(10,10000);
$location = 'images/carimages/c'.$rnd.'.jpg';
$img->save($location);
$answer[] = $rnd;
}
return $answer;
}
公共功能上传(请求$Request)
{
$array=$request->file('image');
$count=计数($array);
对于($i=0;$ifile($image')[$i]);
$img->加宽(800);
$img->fit(800600);
$rnd=兰特(1010000);
$location='images/carimages/c'.$rnd..jpg';
$img->save($location);
$answer[]=$rnd;
}
返回$answer;
}
试试这个
return response()->json($answer);
试试这个
return response()->json($answer);
尝试使用
json\u encode()
这会解决你的问题
public function upload(Request $request)
{
$answer=array();
$array = $request->file('image');
$count = count($array);
for ($i=0 ; $i<$count; $i++)
{
$img = Image::make($request->file('image')[$i]);
$img->widen(800);
$img->fit(800,600);
$rnd = rand(10,10000);
$location = 'images/carimages/c'.$rnd.'.jpg';
$img->save($location);
$answer[] = $rnd;
}
echo json_encode($answer);
die;
}
公共功能上传(请求$Request)
{
$answer=array();
$array=$request->file('image');
$count=计数($array);
对于($i=0;$ifile($image')[$i]);
$img->加宽(800);
$img->fit(800600);
$rnd=兰特(1010000);
$location='images/carimages/c'.$rnd..jpg';
$img->save($location);
$answer[]=$rnd;
}
echo json_编码($answer);
死亡
}
尝试使用json\u encode()
这会解决你的问题
public function upload(Request $request)
{
$answer=array();
$array = $request->file('image');
$count = count($array);
for ($i=0 ; $i<$count; $i++)
{
$img = Image::make($request->file('image')[$i]);
$img->widen(800);
$img->fit(800,600);
$rnd = rand(10,10000);
$location = 'images/carimages/c'.$rnd.'.jpg';
$img->save($location);
$answer[] = $rnd;
}
echo json_encode($answer);
die;
}
公共功能上传(请求$Request)
{
$answer=array();
$array=$request->file('image');
$count=计数($array);
对于($i=0;$ifile($image')[$i]);
$img->加宽(800);
$img->fit(800600);
$rnd=兰特(1010000);
$location='images/carimages/c'.$rnd..jpg';
$img->save($location);
$answer[]=$rnd;
}
echo json_编码($answer);
死亡
}
将数组作为JSONAPPLICABLE的副本返回,而将数组作为JSONAPPLICABLE的副本返回,JSONAPPLICABLE是js中静止字符串的副本。在php中,它的数组ir json是正确的,但当我在js中使用xhr.responseText得到它时,它将转到stringi,我从js代码中解决了它。它应该从javascript转换为json:var obj=json.parse(xhr.responseText);console.log(obj[1]);tnxit仍然是js中的字符串。在php中,它的数组ir json是正确的,但当我在js中使用xhr.responseText得到它时,它将转到stringi,我从js代码中解决了它。它应该从javascript转换为json:var obj=json.parse(xhr.responseText);console.log(obj[1]);请在问题中添加字符串结果请在问题echo json_encode(数组(“answer”=>$answer))中添加字符串结果;死亡代码>并在ajax代码中写入数据类型:'json'
,以便在ajax成功响应echo json_encode(数组(“应答”=>$answer))中作为json获得;死亡代码>并在ajax代码中编写数据类型:'json'
,以便在ajax成功响应中作为json获得