<p>ajax的作用是防止页面重新加载,这就是ajax的魔力所在。因此,如果您提供<code>path</code>,则可以将其用于页面重新加载</p>
<p>Html:
</p>
<pre><code><script>
function OpenModal(){
var fname="Quartz";
var mname="Rohit";
var lname="Patel";
$.ajax({
type:'POST',
url: "{% url 'display_modal' %}",
data: { csrfmiddlewaretoken: '{{ csrf_token }}'},
success: function(param123){
dict1 = JSON.parse(param123);
let data1 = dict1.test;
alert("Successful call do what ever you want");
alert(data1);
},
error: function(){
alert("sorry");
}
});
}
</script>
</code></pre>
<p>URL.py:
它将把正则表达式与您提到的url相匹配。
您在ajax调用中提到的<code>name='display_modal'</code>参数和<code>url: "{% url 'display_modal' %}"</code>是相同的</p>
<pre><code> from django.conf.urls import url
urlpatterns = [
url(r'^display_modal', views.displayModalView, name='display_modal')
]
</code></pre>
<p>views.py:</p>
<pre><code>import json
from django.http import HttpResponse
def displayModalView(request):
#do what ever you want but at the end return Httpresponse with dictionary if you want
to use dictionary in your ajax call.
param123 = {'test': "Hello_world"}
return HttpResponse(json.dumps(param123))
</code></pre>