Skip to content

Instantly share code, notes, and snippets.

@Xifeng2009
Created April 9, 2019 06:07
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 Xifeng2009/e408ae041716041f1e9bcd7b6bda13d0 to your computer and use it in GitHub Desktop.
Save Xifeng2009/e408ae041716041f1e9bcd7b6bda13d0 to your computer and use it in GitHub Desktop.
# In app/views.py
import random
from django.http import JsonResponse
def ajax_1(request):
context = {"data": random.randint(1, 999)}
return JsonResponse(context)
# In templates
<input id="a1" value="AAA">
<button id="b1" class="button">Click!</button>
<script>
$("#b1").click(function () {
$.ajax({
url: '{% url "main:ajax_1" %}',
type: 'get',
async: true,
//data: 1,
//dataType: 'json',
error: function () {
},
success: function (data) {
$("#a1").val(data.data)
}
})
})
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment