Ajax 到 Python 金字塔数据
到目前为止,我已经成功获取了一堆 contentEditable 属性为 True 的 HTML 元素,并将它们的 id 和 HTML 数据连接在一起以生成 Ajax 数据字符串。我可以将序列化数据返回到服务器,没问题。例如,
$(document).ready(function(){
$("#save").click(function(){
var ajax_string = ''
$( "[contenteditable=True]" ).each(function( intIndex ){
ajax_string = ajax_string + '&' + $("[contenteditable=True]")[intIndex].id + ':' + $(this).html();
});
$.ajax({
type:"POST",
url:"/episode_edit/{{ episode.ID_Episode }}",
data:ajax_string,
success:function(result){
<!--alert( ajax_string );-->
}
});
});
});
在服务器上:
for r in request.params: print r
我得到字符串:
AltTitle:some Alt Title
PrintTitle:The Print Title
Notes:A bunch o' notes.
我现在的困境是我需要将每个 request.param
字符串转换为字典对象,以便我可以将其映射回我的数据库模型。我可以想到一些非常丑陋的方法来做到这一点,但最好的方法是什么?
So far, I have managed to take a bunch of HTML elements for whose contentEditable attribute is True and join their id's and HTML data together to make an Ajax data string. I can get the serialized data back to the server, no problem. For example,
$(document).ready(function(){
$("#save").click(function(){
var ajax_string = ''
$( "[contenteditable=True]" ).each(function( intIndex ){
ajax_string = ajax_string + '&' + $("[contenteditable=True]")[intIndex].id + ':' + $(this).html();
});
$.ajax({
type:"POST",
url:"/episode_edit/{{ episode.ID_Episode }}",
data:ajax_string,
success:function(result){
<!--alert( ajax_string );-->
}
});
});
});
On the server:
for r in request.params: print r
I get strings:
AltTitle:some Alt Title
PrintTitle:The Print Title
Notes:A bunch o' notes.
My dilema now is that I need to convert each request.param
string into a dictionary object, so I can map it back to my database model. I can think of some very ugly ways of doing this, but what is the best way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您说您想将 each request.param 字符串转换为字典对象,但这是您的意思吗?看起来每个字符串只是一个键/值对。
您可以使用以下方法非常简单地根据这些值创建字典:
You say you want to convert each request.param string into a dictionary object, but is that what you meant? It looks like each string is just a key/value pair.
You can pretty simply create a dictionary from those values using: