简体   繁体   English

用python进行HTML编码解码

[英]HTML encoding decoding with python

I am sending encoded HTML to my python ajax handler by jQuery. 我正在通过jQuery将编码的HTML发送到我的python ajax处理程序。 Here is my jQuery code: 这是我的jQuery代码:

var animal_data = encodeURI( $('#animal-list-table').html() ); // something like this: %0A%20%20%20%20%20%20%20%20
mydata = 'action=send_mail&animal_data='+animal_data
$.ajax({
    type: 'POST',
    url: '/customer/templates/slakteweb/ajax-handler',
    data: mydata,
    processData: false,
    success: function(result){
        console.log(result);
    }
});

In the ajax handler, I have tried like this: 在ajax处理程序中,我尝试过这样的操作:

import urllib
animal_data = site.param('animal_data')
animal_data_html = urllib.unquote(animal_data).decode('utf8')

But I wanted to print the HTML tags and everything so that I can send it as HTML email. 但是我想打印HTML标记和所有内容,以便可以将其作为HTML电子邮件发送。 What is the best way to do that? 最好的方法是什么?

For POST data you don't need the encodeURI function, since POST data is not sent as part of the URI/URL. 对于POST数据,由于POST数据不是作为URI / URL的一部分发送的,因此您不需要encodeURI函数。 If you don't do that you don't need to decode anything on the server side either. 如果您不这样做,则也不需要在服务器端解码任何内容。

It's generally a very bad idea not validating the input you get from untrusted source (here: browser). 通常,不验证您从不受信任来源(此处为浏览器)获得的输入是一个非常糟糕的主意。 Even if you think you do the whole coding on client-side, consider it unsafe. 即使您认为自己在客户端进行了整个编码,也请认为这是不安全的。 Long story short: the best and commonly used way is parsing the content as plain text, not HTML. 长话短说:最好且常用的方法是将内容解析为纯文本,而不是HTML。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM