2014-11-01 35 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);