0
<header>
    <div class="content-wrapper">
        <div class="float-left">
            <p class="site-title">
                <a href="~/">ASP.NET Web API</a></p>
        </div>
    </div>
</header>
<div id="body">
    <section class="featured">
        <div class="content-wrapper">
            <hgroup class="title">
                <h1>
                    Calculator</h1>
            </hgroup>
        </div>
    </section>
    <section class="content-wrapper main-content clear-fix">
    <input id="id" type="text" />
    <p />
    <input id="Plus" type="button" value="+" /><input id="Minus" type="button" value="-" />
    <p />
    <input id="id2" type="text" />
    <p />
    ________________________________

    <p />
    <label id="answer"></label>
    </section>
</div>

this is my cshtml document and I need to have the label answer show a value from when I click plus which will then go to my api an get /api/add?id=value&id2=value then it just gets and XML document and I can't do anything with this XML document. How do I display the value from the XML document for answer and IT CAN NOT USE JSON, it has to be XML output and using javascript is ok.

4
  • did you tried to parse your xml into json and use json data in your mvc view(cshtml document)? alternative way would be parsing your xml content to viewmodel and passing view-model to mvc view. Commented Jun 19, 2012 at 20:36
  • I can not use json ever! Commented Jun 19, 2012 at 20:37
  • Did you tried to parse your xml content to a viewmodel and pass view-model to your MVC view. Commented Jun 19, 2012 at 20:41
  • do it through $.ajax({} , look at provided example. Commented Jun 20, 2012 at 2:10

1 Answer 1

2

If you use jquery you can do

<script>

 $('#buttonID').click(function(e){
 e.preventDefault();
 $.ajax({

  url : "url/to/api?params=1",
  dataType : "xml",
  success : function(data){

   var dataAsXml = $.parseXml(data);

   var answer = $(dataAsXml).find("answerNode");

    $('#answerLabelID').text(answer);

  });   // end Ajax call added paren and semi
}); // end button click




</script>
Sign up to request clarification or add additional context in comments.

3 Comments

Here is a link to show you how to get to the node you want in the xml returned stackoverflow.com/questions/2716517/jquery-xpath-find
Can you verify that your API is returning xml?
Can you verify that your API is returning xml? also keep in mind that the IDs I put in for the answer id label and your button is need to be the IDs you are using. In addition the $.ajax function depends on jQuery being loaded. encosia.com/… . I recommend you learn/use firebug for Firefox specifically the javascript console and network panel for debugging Ajax calls.

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.