0

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

2 Answers 2

1

You don't need a view. You could return the XML directly from your controller action:

[HttpPost]
public ActionResult PortfolioList()
{
    string xml = ...
    return Content(xml, "text/xml");
}

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.

Sign up to request clarification or add additional context in comments.

6 Comments

thank you for responding. I've already tried to return Content(..), but the XML gets rendered in the browser directly. I'm missing a key piece of code somewhere...
maybe I should try NOT using a View file; maybe just a straight HTML file.
No, you don't need a view at all.
You have 2 controller actions, right? One that serves the view containing the AJAX request and the PortfolioList controller action which returns a Content as shown in my answer?
Ahhhh, interesting point ! No, I don't have 2 views to handle this. I'm doing it all in the PortfolioList controller. So it sounds like I need to write a separate controller and call into it from $.ajax() something like this: url: "/Portfolios/RetrievePortfolio" ???
|
0

You probably need to specify the dataType as XML in your .ajax() setup as seen below

$.ajax({
            url: "/Portfolios/PortfolioList",
            type: "POST", 
            //add this line
            dataType: "XML",
            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");
            }

        });

There are other options as well from the JQuery documentation on this function depending on your needs

6 Comments

thank you as well, but it's still rendering the XML doc directly in the browser (IE in my case). It seems my Controller is NOT returning the XML back to the jQuery ajax routine.
@bob Yes, I have seen the same issue (with JSON) and Jquery not handling it. How are you calling the ajax function? Are you getting any JS errors in your dev console?
I'm calling the ajax function just as you re-posted in your answer (with dataType: "XML"), EXCEPT I modified the URL as "/Portfolios/getPortfolios". And yes, I'm crashing on js line: var $title = $xml.find("portfolioSummary").text(); (null or undefined).
funny thing is that when I crash, I'm able to use the VS IDE to view my vars. and when I use the Watch window to view the "data" object returned, I CAN INDEED see the XML document returned. Is it something with $.parseXML(data) ???
OKAY NO CRASHES NOW ! I simply removed the [HttpPost] attrib from my action method inside the controller. So now public ActionResult looks basically like this : getPortfolios() { var portf = new PortfolioResponse { XmlPortfolioResponse = xmlResponse }; return Content(portfoliosResponse, "text/xml"); }
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.