javascript - Getting Stock Informatin using YQL and JQuery in ASP.NET -


i trying retrieve stock information using yql query in asp.net project. using jquery make json call retrieve data. issue when click 'getdata' button on page, refreshes page , no data gets displayed. if run project in debug mode in visual studio, returns stock information , displays on page end of debug clears page. here code:

    <html xmlns="http://www.w3.org/1999/xhtml">     <head runat="server">         <title></title>         <script src="scripts/jquery-1.11.0.min.js" type="text/javascript"></script>          <script type="text/javascript">             function getdata() {                 var url = "http://query.yahooapis.com/v1/public/yql";                 var data = encodeuricomponent("select * yahoo.finance.quotes symbol in ('msft')");                 $.getjson(url, 'q=' + data + "&format=json&diagnostics=true&env=http://datatables.org/alltables.env")             .done(function (data) {                 $("#result").text("bid price: " + data.query.results.quote.lasttradepriceonly);                 alert(data.query.results.quote.lasttradepriceonly);             })             .fail(function (jqxhr, textstatus, error) {                 var err = textstatus + ", " + error;                 $("#result").text('request failed: ' + err);             });             }          </script>     </head>     <body>          <form id="form1" runat="server">                 <div id='result'>no price</div>             <button type="submit" onclick="getdata();">get data</button>                  </form>     </body>     </html> 

jsfiddle: http://jsfiddle.net/a3tup/

does know why data gets cleared though data gets returned in json call?

thanks.

the form submitted submit-action on button try re-factor , add .preventdefault()

html

 <form id="form1" runat="server">             <input type="text" id="symbol" />         <div id='result'>no price</div>         <button type="submit">get data</button>     </form> 

js

function getdata(e) {     //prevent sumbitting form      e.preventdefault()     var url = "http://query.yahooapis.com/v1/public/yql";     var symbol = $("#symbol").val();     var data = encodeuricomponent("select * yahoo.finance.quotes symbol in ('bgg')");      $.getjson(url, 'q=' + data + "&format=json&diagnostics=true&env=http://datatables.org/alltables.env")         .done(function (data) {         $("#result").text("bid price: " + data.query.results.quote.lasttradepriceonly);     })         .fail(function (jqxhr, textstatus, error) {         var err = textstatus + ", " + error;             $("#result").text('request failed: ' + err);     }); }  //bind submit event $(document).on("submit", "#form1" getdata) 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -