【问题标题】:Pass base 64 string using ajax script使用 ajax 脚本传递 base 64 字符串
【发布时间】:2015-08-07 15:00:12
【问题描述】:

我已经使用以下脚本将字符串 url 传递到另一个 php 页面。

            function saveIt(){

                var xmlhttp = new XMLHttpRequest();

                xmlhttp.onreadystatechange = function() {
                    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                        alert("Save successfully!");
                    }
                }

                var value1 = document.getElementById("url").value ;
                var value2 = document.getElementById("time").value;
                var value3 = document.getElementById("note").value;

               xmlhttp.open("POST", "insertFrame.php", true);
               xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
               xmlhttp.send("url="+ value1 + "&time=" + value2 + "&note=" + value3);



            }

之后,我将检索到的数据插入到表中

$url = $_POST['url'];
$time = $_POST['time'];
$note = $_POST['note'];

mysql_connect("localhost", "root", "");
mysql_select_db("studioassessor_01");

mysql_query("INSERT INTO frame VALUES ('$url', '$time', '$note')");

但是,当我想从 db 中检索 url 以显示图像时,它失败了,看起来像是损坏了。

    <?php

        $con = mysql_connect("localhost", "root", "");
        mysql_select_db("studioassessor_01");

        $sql = mysql_query("SELECT * FROM frame");

        while($row = mysql_fetch_array($sql)){
            echo '<img src="' . $row['url'] . '">';

            echo "<br/><br/>";
            echo $row['note'];
        }

    ?>

在我更改代码和测试之后,我发现 xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");是当传递到另一个 php 页面并存储在数据库中时使我的 url 变为不同值的可能原因。请问我该如何解决这个问题?

【问题讨论】:

  • base64_decode 将 url 编码的字符串转换回字节,因此如果 $url 是 base64 编码的字符串并且您想将其用作图像源,只需将其回显,不要解码它.
  • 我尝试在不解码的情况下直接回显它,但仍然无法显示图像。所以,我想知道 setRequestHeaderPart 是否让我的 url 在发送到另一个 php 页面进行存储时看起来不同。
  • 有人可以帮忙吗?谢谢。

标签: php ajax post base64 content-type


【解决方案1】:

我已经使用 str_replace(' ', '+', "your string") 解决了我的问题。这是因为编码后的字符串值将全部变为空格并且缺少 + 号。因此,我需要将其替换回来以获取正确的 url 值并显示图像。谢谢。

【讨论】:

    猜你喜欢
    • 2015-08-09
    • 2013-07-09
    • 1970-01-01
    • 2017-02-03
    • 1970-01-01
    • 1970-01-01
    • 2011-03-04
    • 2011-08-25
    相关资源
    最近更新 更多