Created
March 27, 2017 05:24
-
-
Save joelburton/b2ccea52a73babe326b6a87601e8111b to your computer and use it in GitHub Desktop.
AJAX movie rating snippet
This file contains 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
AJAX version: | |
<form action="HANDLED BY AJAX" id="rate-movie-form"> | |
<label>Rating: | |
<input type="number" name="score" id="score"> | |
</label> | |
<input type="submit" value="Rate"> | |
</form> | |
<script> | |
"use strict"; | |
function reportServerScore(result) { | |
console.log(result); | |
if (result.status == "error") { | |
alert("ERROR! " + result.detail); | |
} else { | |
$("#current-rating").html(result.score); | |
} | |
} | |
function sendScore(evt) { | |
evt.preventDefault(); | |
var score = $("#score").val(); | |
$.post("/movies/ajax/{{ movie.movie_id }}", | |
{"score": score}, | |
reportServerScore); | |
} | |
$("#rate-movie-form").on("submit", sendScore); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment