【问题标题】:Ajax load issues with space in urlAjax 加载 url 中的空格问题
【发布时间】:2015-04-29 01:16:15
【问题描述】:

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

python.py 包含来自 mysql 数据库的记录的表。

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

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

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

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

一些细节

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

【问题讨论】:

    标签: javascript python ajax mod-python


    【解决方案1】:

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

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

    你可能会取得更好的成功。

    【讨论】:

    • 谢谢。那行得通:)奇怪的是,我试图将“”替换为“%20”,这给了我空结果:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多