Ajax url 中 space 的加载问题

Ajax load issues with space in url

我有 2 个 python 文件:gui.py & index.py

python.py 包含 table 以及来自 mysql 数据库的记录。

gui.py 包含 python.py 和带有发送消息按钮的文本字段。

此外,gui.py 包含 javascript,它将加载带有参数的新 python.py

gui.py 脚本

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("#content").load("http://localhost/index.py");
    $("button").click(function(){
        var url = "http://localhost/index.py?name=1&message=";
        var msg = document.getElementById("message").value;
        var res = url.concat(msg);
        alert(res); //here I always can understand that result URL is okay.
        $("#content").load(res);
    });
</script>

index.py部分

cursor.execute("INSERT INTO `messages` (`sender`, `receiver`, `text`, `time`) VALUES ('" + req.form['name'] + "', '3', '" + req.form['message'] + "', now())")
});

其中 req 来自 def index(req): 我正在使用 mod_python.

所以,问题是 - 当我尝试用 space 发送消息时 - 我的 table 消失了(直到下一页刷新(但是如果没有 spaces,我不需要刷新它,ajax 加载在这里很好))。然后我将只看到消息的第一个词。正如您在我的代码中看到的那样,如果我复制该警报,我有 alert(res); 并且 运行 url - spaces 工作得很好。

一些细节

我正在使用 ubuntu 14.04,apache2,mod_python,python 2.7,mysql 数据库,python-MySQLdb。

您应该 URL 对邮件正文进行编码以防止 URL 中出现特殊字符,例如 space。如果你这样做了

var msg = encodeURIComponent( document.getElementById("message").value );

你可能会有更好的成功。