Skip to content

Instantly share code, notes, and snippets.

@ykyuen
Created April 18, 2019 09:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ykyuen/b5189717afba286fc34eac617bfb8648 to your computer and use it in GitHub Desktop.
Save ykyuen/b5189717afba286fc34eac617bfb8648 to your computer and use it in GitHub Desktop.
handling-http-request-in-go-echo-framework-2-03
{{define "title"}}
Boatswain Blog | {{index . "name"}}
{{end}}
{{define "body"}}
<h1>{{index . "msg"}}</h1>
<h2>This is the about page.</h2>
<!-- HTML form -->
<form id="post-full-name-form">
<input type="text" id="first-name" placeholder="First Name">
<input type="text" id="last-name" placeholder="Last Name">
<button type="button" id="submit-btn">Submit</button>
</form>
<!-- Print the response after the jQuery ajax request -->
<pre id="form-result"></pre>
<script type="text/javascript">
// Send a ajax request when the submit button is clicked
$("#submit-btn").on("click", function(){
var firstName = $("#first-name").val();
var lastName = $("#last-name").val();
$.ajax({
type: "POST",
url: "/api/post-full-name",
data: JSON.stringify({
first_name: firstName,
last_name: lastName
}),
processData: false,
contentType: "application/json",
dataType: "json"
}).done(
function(data){
$("#form-result").text(JSON.stringify(data, null, 2));
}
).fail(
function(data){
$("#form-result").text("POST request failed!");
}
)
});
</script>
{{end}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment