In my MVC view (using VS 2012 RC), I’m attempting to use jQuery to parse XML data returned from my Controller. However, my focus is to make use of the jQuery $.ajax() function.
Problem is that ajax is NOT returning an XML Doc from my controller; it returns some formatted Html.
Example:
I can indeed return it directly to my view inside a , but I don’t want that entire XML doc displayed that way. For example, get XML from the @Model object:
@model RazorMvc.Models.PortfolioResponse
@{
ViewBag.Title = "PortfolioList";
}
...
<div class="XmlResp">
<@Html.Raw(@Model.XmlPortfolioResponse.OuterXml.ToString())>
</div>
HOWEVER, I would much prefer to use $.ajax() to retrieve my XML Doc. The only problem is that the return value from my Controller code is NOT the XML; it’s returning some formatted HTML text:
This is a test div !!!
What’s getting displayed is some formatted HTML as follows:
Wow are we getting somewhere ?!!! My company SolutionsRegisterLog inHomeAboutContactPortfoliosPortfolioListPortfolio ListingThis is a test div !!!© 2012 – My ASP.NET MVC ApplicationFacebookTwitter
Here is the jQuery code. I would hope to get an XML doc in the DATA object:
<script type="text/javascript">
$.ajax({
url: "/Portfolios/PortfolioList",
type: "POST",
async: true,
success: function(data){
if (!data)
alert("No xml data returned.");
else{
var $xml = $(data);
var $title = $xml.find("portfolioSummary").text();
$('.xmlTest').text("Wow are we getting somewhere ?!!!");
$('.xmlTest').append($xml.text());
alert("success!");
}
},
error: function (error) {
alert("failed");
}
});
Some guidance and advice would be greatly appreciated.
Thanks,
Bob
You don’t need a view. You could return the XML directly from your controller action:
Now, since you have set the proper content type response header, jQuery will know how to parse it and in your success callback you will be able to directly work with this XML stream.