简体   繁体   English

如何将数组从php(使用json_encode)传递给javascript

[英]How to pass an array from php (using json_encode) to javascript

My java script. 我的java脚本。 I want to display image. 我想要显示图像。 the javascript require image path as array format. javascript需要图像路径作为数组格式。 I tried to supply the path throw ajax. 我试图提供路径抛出ajax。 its not working. 它不起作用。 when i use hard code its working. 当我使用硬编码工作。 my javascipt below. 我的javascipt如下。 its not working. 它不起作用。 the working code supply below the javascript and my php file code also there. javascript下面的工作代码供应和我的php文件代码也在那里。

$(function () {
    $.get( "php/building_edit_image_get_db.php", function( your_return_data ) {
    alert(your_return_data);

        $("#editimagefile").fileinput({
            showUpload: false,
            showCaption: false,
            overwriteInitial: true,
            initialPreview: [your_return_data],
            initialPreviewAsData: true, // identify if you are sending preview data only and not the raw markup
            initialPreviewFileType: 'image', // image is the default and can be overridden in config below
            browseClass: "btn btn-primary btn-lg",
            allowedFileExtensions : ['jpg', 'png','gif']
        });
    });
});

It's not working. 它不起作用。 when i place hardcode its working properly the script is below 当我把硬编码放在正常工作时,脚本在下面

$(function () {
    $.get( "php/building_edit_image_get_db.php", function( your_return_data ) {
    alert(your_return_data);

        $("#editimagefile").fileinput({
            showUpload: false,
            showCaption: false,
            overwriteInitial: true,
            initialPreview: [
                "http://lorempixel.com/800/460/people/1",
                "http://lorempixel.com/800/460/people/2"
            ],
            initialPreviewAsData: true, // identify if you are sending preview data only and not the raw markup
            initialPreviewFileType: 'image', // image is the default and can be overridden in config below
            browseClass: "btn btn-primary btn-lg",
            allowedFileExtensions : ['jpg', 'png','gif']
        });
    });
});

My php file for returing array value. 我的php文件用于恢复数组值。

session_start();
require_once ('../aiboc_admin/class/Buidling_Image.php');

$editid = $_SESSION['BUILD_LIST_EDIT_ID'];

$getimgs = Buidling_Image::GetGalleryImageByID($editid);

    foreach ($getimgs as $setimgs)
    {
        $imgs[] = $setimgs['img_url'];
    }
echo json_encode($imgs,JSON_UNESCAPED_SLASHES);

You should use $.parseJSON() since you're getting json format when you use json_encode : 你应该使用$.parseJSON()因为你在使用json_encode时获得了json格式:

$.get( "php/building_edit_image_get_db.php", function( your_return_data ) {

    $("#editimagefile").fileinput({
        showUpload: false,
        showCaption: false,
        overwriteInitial: true,

        initialPreview: $.parseJSON(your_return_data),

        initialPreviewAsData: true, // identify if you are sending preview data only and not the raw markup
        initialPreviewFileType: 'image', // image is the default and can be overridden in config below
        browseClass: "btn btn-primary btn-lg",
        allowedFileExtensions : ['jpg', 'png','gif']
    });

});

Or you could use $.getJSON() instead of $.get() request, then you don't need to parse it : 或者您可以使用$.getJSON()而不是$.get()请求,然后您不需要解析它:

$.getJSON( "php/building_edit_image_get_db.php", function( your_return_data ) {

Hope this helps. 希望这可以帮助。

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

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