2014-11-01 33 views
0

我有一个返回XML的web服务。即获得XML使用jQuery显示AJAX请求的结果

代码:

function PostToService() 
{ 
    $.ajax({ 
     type: 'POST', 
     url: "http://localhost:53247/api/student/studentstatus", 
     dataType: 'xml', 
     data: "<StudentStatus><ID>C101</ID><Score>56</Score></StudentStatus>", 
     contentType: "text/xml", 
     Accept: "text/xml", 
     success: function (data) { 
      console.log(data); 
     } 
    }); 

我可以看到Devtools控制台返回的XML扩展#document:

<UpdatedStudent> 
    <StudentID>C101</StudentID> 
    <Result>Pass</Result> 
<UpdatedStudent> 

尝试创建与响应的ID的股利和使用该代码:

$("#response").html(data); 

在控制台中获取此错误:

Cannot read property 'ownerDocument' of null 

如何向用户显示响应?

在此先感谢。

回答

0

当你的代码:

$("#response").html(data); 

您的信息是,你说你希望XML用作HTML文本,这将无法工作,除非XML的确是一个HTML文档的XML文本。浏览器将尝试将XML解释为HTML并失败。如果您只想显示XML,请尝试以下操作:

$("#response").text(data);