摘要:AJAX 範例1
urls.py
url(r'^WebAPI$', 'app.views.WebAPI', name='WebAPI'),
view.py
def WebAPI(request): assert isinstance(request,HttpRequest) ss='no' if(request.is_ajax()): ss='yes' return HttpResponse(ss)
test.html
{% extends "app/layout.html" %}
<script src="../../static/app/scripts/jquery-1.10.2.min.js"></script>
{% block content %}
<span>AJAX:</span><span id="ajaxString">No</span>
<a href="#" id="Test">測試</a>
{% endblock %}
{% block defJavascript %}
<script type="text/javascript">
$(function () {
$('#Test').on('click', function () {
$.ajax({
type: "GET",
url: "/WebAPI",
data: {},
success: function (response) {
$('#ajaxString').text(response);
}
});
});
})
</script>
{% endblock %}