【问题标题】:Echo-ing back data from php to ajax and print it将数据从 php 回显到 ajax 并打印出来
【发布时间】:2018-10-06 00:39:13
【问题描述】:

嗨,这是我第一次在这里发帖,我需要一些帮助,我在将结果从 php 回显到 ajax 时遇到问题。我想在创建新画布并存储到服务器后显示文件名。

AJAX

                    $.ajax({
                        url: 'save_map.php',
                        data: { img_data:img_data },
                        type: 'post',
                        dataType: 'json',
                        success: function (response) {
                        window.location.reload();

                        }

我想通过这个 PHP 打印刚刚创建的新图像的名称。我想获取在 $filename 中创建的字符串值,然后打印它。

PHP

<?php 

    $result = array();
    $imagedata = base64_decode($_POST['img_data']);
    $filename = md5(date("dmYhisA"));
    //Location to where you want to created sign image
    $file_name = './doc_map/'.$filename.'.png';
    file_put_contents($file_name,$imagedata);
    $result['status'] = 1;
    $result['file_name'] = $file_name;
    echo json_encode($result);


?>

【问题讨论】:

    标签: php jquery ajax


    【解决方案1】:

    请求成功后您正在重新加载页面。 您应该使用响应变量来显示您从 PHP 代码返回的任何内容。

       success: function (response) {
       window.location.reload();
       }
    

    试试吧:

       success: function (response) {
       alert(response.file_name);
       }
    

    【讨论】:

    • 我试过这个,它会引发 [object Object] 处于警报状态。我对 ajax 很陌生
    • 那是因为您的响应是您的 dataType 参数中指定的 json 对象。这不是错误,试试 response.file_name
    • @RuzMaharjan response 是一个 JSON 对象,您需要访问它。 stackoverflow.com/questions/10895306/…
    猜你喜欢
    • 2020-03-27
    • 2012-05-25
    • 1970-01-01
    • 2021-01-06
    • 2021-10-04
    • 1970-01-01
    • 2011-09-30
    • 1970-01-01
    • 2015-11-01
    相关资源
    最近更新 更多