-
-
Save Xifeng2009/e408ae041716041f1e9bcd7b6bda13d0 to your computer and use it in GitHub Desktop.
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
# 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