0

有什么方法可以从 python HttpResponse 获取 xml 数据(通过 Javascript)并将其放入网页上的 body 元素中?

python中有我的xml示例:

import xml.etree.ElementTree as ET

root = ET.Element("table")

tr = ET.SubElement(root, "tr")
td = ET.SubElement(tr, "td")
td.text = "some text"
tree = ET.ElementTree(root) 

response = HttpResponse(tree)

JS:

$(document).ready(function() {
  $.get("http://localhost:8000/getXml", function(data){
    alert(data);
  });      
});

感谢帮助。

4

2 回答 2

0

HTML 是 XML,因此您作为 HTTP 响应返回的任何 HTML(部分,而不是完整页面,即没有<head><body>标签)都可以插入网页中。

这就是 ajax 请求的作用。

您想使用 XML API 构建响应吗?虽然你可以这样做,但 django template 非常适合。以任何方式构建响应,获取字符串表示并作为响应返回。

于 2012-11-06T08:34:20.840 回答
0

I agree with Rohan that you should research Python if you're trying to serve data from Python to a web-client, or as a mixture of HTML/CSS/Javascript, or what have you. Check this Django tutorial out. It's awesome and only took me a few days to get through, and allows for a lot of different applications. The Onion, if you've ever heard of it apparently uses Django.

于 2012-11-06T08:40:20.950 回答