Created
April 18, 2019 09:34
-
-
Save ykyuen/b5189717afba286fc34eac617bfb8648 to your computer and use it in GitHub Desktop.
handling-http-request-in-go-echo-framework-2-03
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{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