Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在php函数中返回数组_Javascript_Php_Ajax_Laravel_Intervention - Fatal编程技术网

Javascript 如何在php函数中返回数组

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++)

我使用Laravel5.4和干预插件将图像上传为ajax

我将在php控制器上传图像,它将返回一个响应(文件名)

php返回的变量是一个数组,但在javascript中它是字符串,我无法迭代

 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获得