2009-11-16 74 views
0

我的程序給了我一個來自jquery的錯誤。我不明白爲什麼。我工作在C#中,但在jQuery的它不如何將XML從C#返回給JQuery

using (XmlTextWriter writer = new XmlTextWriter(new StringWriter(sb))) 
      { 
       writer.Formatting = System.Xml.Formatting.Indented; 
       ser.Serialize(writer, ct); 
       XMLContent = sb.ToString(); 
      } 
      return Content(XMLContent, System.Net.Mime.MediaTypeNames.Text.Xml); 

˚F

$.ajax(
     { 
      type: "POST", 
      url: action, 
      data: formobj, 
      dataType: "xml", 
      success: function(result) { 
       alert(result); 
      }, 
      error: function(req, status, error) { 
       alert(req.statusText); 
      } 
     }); 
     return false; 

當我更換

return Content(XMLContent, System.Net.Mime.MediaTypeNames.Text.Xml); 

return Content(XMLContent); 

,並刪除

dataType: "xml", 

jquery it all works。

+0

你得到什麼錯誤? – 2009-11-16 07:31:48

+0

在JQuery Ajax Part中,req.statusText值返回OK。所以我很困惑它可能是什麼 – Luke101 2009-11-16 07:45:45

回答

2

一種可以簡化和優化ajax的方法很多,就是使用JSON而不是XML。

(除非你真的想你的結果是XML)

在ASP.Net MVC可以讓你的動作返回JSON作爲結果。

return Json(new { 
    variableName: someData, 
    anotherVariableName: someMoreData 
}); 

在您的JS:

$.post(
    'yourActionName', 
    optionalData, 
    function(d) { 
     alert(d.variableName); 
     alert(d.anotherVariableName); 
    } 
); 

得不能再簡單的比這:)