2012-08-11 39 views
1

我试图使用jQuery的阿贾克斯从Python脚本返回XML数据返回XML时:parseerror jQuery中从mod_python的

<html><head> 
    <script type="text/javascript" src="jquery-1.7.2.js"></script> 
    <script type="text/javascript"> 
    $(document).ready(function() { 
     $.ajax({ 
     type: "POST", 
     url: "script.py/method", 
     dataType: "xml", 
     success: function(xml) { alert('Success!'); }, 
     error: function(request, error) { alert(error); } 
     }); 
    }); 
    </script> 
</head><body></body></html> 

当我script.py返回任何警报显示Success!,但只要我尝试添加一些XML数据,我得到的是parseerror。我应该如何返回XML而不会得到parseerror?

script.py - 工作

def method(req): 
    req.content_type = 'text/xml' 
    req.write('') # Works! 

script.py - 碎

def method(req): 
    req.content_type = 'text/xml' 
    req.write('<item>1</item><item>2</item>') # parserror! 

回答

2

你的XML是无效的,你缺少一个根节点(一个根节点),例如

def method(req): 
    req.content_type = 'text/xml' 
    req.write('<items><item>1</item><item>2</item></items>') 
+0

所以很明显..我正在寻找另一个方向:) – Qiau 2012-08-11 22:02:19