第一句子网 - 唯美句子、句子迷、好句子大全
第一句子网 > php json_encode 后直接复制给js怎么调用 如何将数组从php(使用json_encode)传递给javascript...

php json_encode 后直接复制给js怎么调用 如何将数组从php(使用json_encode)传递给javascript...

时间:2023-06-26 05:09:08

相关推荐

php json_encode 后直接复制给js怎么调用 如何将数组从php(使用json_encode)传递给javascript...

我的java脚本.我想要显示图像. javascript需要图像路径作为数组格式.我试图提供路径抛出ajax.它不起作用.当我使用硬编码工作.我的javascipt如下.它不起作用. 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']

});

});

});

它不起作用.当我把硬编码放在正常工作时,脚本在下面

$(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: [

"/800/460/people/1",

"/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']

});

});

});

我的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);

解决方法:

你应该使用$.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']

});

});

或者您可以使用$.getJSON()而不是$.get()请求,然后您不需要解析它:

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

希望这可以帮助.

标签:jquery,php,javascript

来源: https://codeday.me/bug/0701/1352216.html

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。