简体   繁体   English

我无法获得数组 json 响应

[英]I can not get the array json response

I am trying to send with PHP and jQuery an array response, but it is not working my PHP code is:我试图用 PHP 和 jQuery 发送一个数组响应,但它不起作用我的 PHP 代码是:

public function get(Request $request)
{
    $pre_images = Pre_Image::all();

    foreach ($pre_images as $pre_image) {
        $data[] = array("file" => $pre_image->image);
    }

    echo json_encode($data);
}

It creates the array and I encode it to be sent.它创建数组,然后我对其进行编码以发送。 Then my jQuery is:然后我的 jQuery 是:

$( 'body' ).on( "click", '#add_value', function() {
    $.ajax({
        type: 'POST',
        url: '/pre_image/get',
        success:function(files)
        {
            var len = files.length;

            var values = '';

            for(var i=0; i<len; i++){
                values = values + '<div class="col-md-3">'+ files[i].file +'<div class="col-md-3">';
            }

            $(".values").append('<div class="row" style="padding-left:20px; padding-right:20px;"><div class="col-md-3"><div class="form-group"><br><label class="control-label mb-10">Nombre de las Opciones (Ejem: Rojo, Azul.)</label><input type="text" class="form-control" name="value_name"></div></div><div class="col-md-3"><div class="form-group"><br><label class="control-label mb-10">Opciones (Ejem: 38, 39, 40.)</label><input type="text" class="form-control" name="value_name"></div></div><div class="col-md-6"><div class="form-group"><br><label class="control-label mb-10">Imagenes </label>'+values+'</div></div></div>');
        }
    });

    return false;
});

I get the response in the success but the first problem that I get it's when I use files.length;我得到了成功的响应,但我得到的第一个问题是当我使用 files.length; it counts 37 and it should count 1 because in this moment the database just has one record, but it is count the leght of the word I mean 272621621287272.jpg it is count how length is the word.它计数 37,它应该计数 1,因为此时数据库只有一条记录,但它是计算单词的长度,我的意思是 272621621287272.jpg 它是计算单词的长度。

The second problem that I get it's that I use to read the array, and it returns me undefined.我得到的第二个问题是我用来读取数组,它返回我未定义。 How is it possible if it has a record stored in the array which it comes from PHP response.如果它在来自 PHP 响应的数组中存储了一条记录,那怎么可能。

I hope you can help me Thanks.jquery我希望你能帮助我Thanks.jquery

Because jquery is not parsing the json into the object.因为 jquery 没有将 json 解析为对象。

There couple ways to say you are expecting json response to jquery.有几种方法可以说您期待对 jquery 的 json 响应。

First one is you should tell you are expecting an json response with dataType第一个是你应该告诉你期待一个带有dataType的 json 响应

$.ajax({
    type: 'POST',
    url: '/pre_image/get',
    dataType: 'json' ,
    success:function(files)
    // and so on

Or another way is add Content-Type = application/json to response header.或者另一种方法是将 Content-Type = application/json 添加到响应标头。 In this way jquery will guess your data type.这样 jquery 会猜测你的数据类型。

in laravel you can do this using json method of response class instead of encoding your data manually.在 Laravel 中,您可以使用响应类的json方法来执行此操作,而不是手动编码您的数据。

public function get(Request $request)
{
    $pre_images = Pre_Image::all();

    foreach ($pre_images as $pre_image) {
        $data[] = array("file" => $pre_image->image);
    }

    return response()->json($data);
}

Or you can manually parse your json response using JSON library.或者您可以使用 JSON 库手动解析您的 json 响应。 I don't recommend this way.我不推荐这种方式。 Ajax able to parse json response unless you tell him you are expecting one. Ajax 能够解析 json 响应,除非你告诉他你期待一个。 No need to parse manually无需手动解析

success(json) {
    var files = JSON.parse(json);
    var values = '';
    files.forEach(function(file){
        values += <div class="col-md-3">'+ file +'<div class="col-md-3">
    })

    $(".values").append('<div class="row" style="padding-left:20px; padding-right:20px;"><div class="col-md-3"><div class="form-group"><br><label class="control-label mb-10">Nombre de las Opciones (Ejem: Rojo, Azul.)</label><input type="text" class="form-control" name="value_name"></div></div><div class="col-md-3"><div class="form-group"><br><label class="control-label mb-10">Opciones (Ejem: 38, 39, 40.)</label><input type="text" class="form-control" name="value_name"></div></div><div class="col-md-6"><div class="form-group"><br><label class="control-label mb-10">Imagenes </label>'+values+'</div></div></div>');

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM