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.

Yusubov
  • 5,815
  • 9
  • 32
  • 69
wizage
  • 304
  • 4
  • 20

1 Answers1

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>
Alex
  • 502
  • 6
  • 14
  • Here is a link to show you how to get to the node you want in the xml returned http://stackoverflow.com/questions/2716517/jquery-xpath-find – Alex Jun 19 '12 at 22:20
  • Can you verify that your API is returning xml? – Alex Jun 20 '12 at 23:31
  • 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. http://encosia.com/3-reasons-why-you-should-let-google-host-jquery-for-you/ . I recommend you learn/use firebug for Firefox specifically the javascript console and network panel for debugging Ajax calls. – Alex Jun 20 '12 at 23:39