javascript - JQuery Ajax call to be triggered onClick -
i new javascript, jquery , ajax. trying do:
i have button in html code, , want trigger ajax call make request web server running on local machine.
this have far:
js code:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script> <script type="text/javascript"> $('call').click(function() { alert("hiii"); $.ajax({ 'url' : 'localhost:8080/blah/blah/blah', 'type' : 'get', beforesend: function() { alert(1); }, error: function() { alert('error'); }, 'success' : function(data) { if (data == "success") { alert('request sent!'); } } }); }); </script>
html code:
<body> <div> <form> <div class = "buttons"> <input type="submit" id="call" value="call"> </div> </form> </div> </body>
can me? in advance!
you have few issues code. one, selector not pointing @ class or id. need use .call
or #call
class or id respectively. second, script has no document.ready
function. check out code below.
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script> <script type="text/javascript"> // document.ready function $(function() { // selector has . class name , # id $('.call').click(function(e) { e.preventdefault(); // prevent form reloading page alert("hiii"); $.ajax({ 'url' : 'localhost:8080/blah/blah/blah', 'type' : 'get', beforesend: function() { alert(1); }, error: function() { alert('error'); }, 'success' : function(data) { if (data == "success") { alert('request sent!'); } } }); }); }); </script>
Comments
Post a Comment