I’m attempting to send a GET request as soon as a button is pressed. As I’m quite inexperienced with HTML and JQuery/Ajax, I’m a little bit confused as to how to do this. My code is as following
<div id = "Reset">
<!-- <form method="get"></form> -->
<!-- <form action="https://wt.ops/reset" method="get"> -->
<button>Reset</button>
<!-- </form> -->
</div>
<script>
$("Reset").ready(function(){
$( "#Reset" ).click(function(){});
$.get( "https://wt.ops/reset", function( data ) {
$( ".result" ).html( data );
alert( "Load was performed." );
})});
</script>
Currently what it’s doing is that it just loads the JQuery scripts automatically when the page is reloaded. But when I click the button, it does nothing.
4
Answers
You should attach the click handler after the document is loaded.
Your
$.get
should be within the click handler.Few things you have to update
1. this is just to ask have you added the jquery script in head
2. You have to change the id from div to button tag or remain as it is
3. You have to update the script function to below function. I have only changed the url to check the data
I see you use Jquery.
https://jsfiddle.net/eaLjb1n5/
Maybe it help you
First, you add jQuery in your html file. We use ajax method for send request.
Here is my code
if you have form data then you can submit form like this