ajax请求django,jquery
#home.html
<html>
<head>
<script type="text/javascript">
$(window).load(function() {
alert("Example of a basic alert box in jquery");
$.ajax({
    type: 'POST' ,
    url: '{% url "filter.views.myajax" %}',
    datatype: 'json' ,
    async: true,
    data:{
        csrfmiddlewaretoken: '{{ csrf_token }}',
        sentence: $('#word').val()
    },

    success: function(json) {
        $('#output').html(json.message);
    }
  });

  }



});

</script>
<title> My first </title>
<body>
<div id="output"> &nbsp; </div>
<div>
<form onsubmit="return false;">
{% csrf_token %}
Enter :- <br />
<input type="text" id="word" /> <br />
<button onclick="callajax()"> Submit </button>
</form>
</div>
</body>
</html>

#views.py
def myajax(request):
sentence= request.POST.get("sentence","")
response_data={}

try:
    response_data['title']='Hey its done ajax'
    response_data['message']=fil(sentence)

except:
    response_data['title']='NO'
    response_data['message']='NO'

return HttpResponse(json.dumps(response_data),content_type="application/json")
#url.py
from django.conf.urls import patterns, include, url

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
# Examples:
url(r'^hello/$', 'filter.views.hello'),
url(r'^ajax/$', 'filter.views.myajax'),
)

I have been trying to implement ajax through jquery and update a div according to the response. There is no output shown when i click submit button. I have added the view.py and my template file here. What is the issue here ?

1个回答


替换:</ p>

  url:'filter.views.myajax'
</ code> </ pre>

使用</ p>

  url:'{%url filter.views.myajax%}'

#**对于Django> = 1.5 **

网址:“ {%网址“ filter.views.myajax”%}”
</ code> </ pre>

内部 $。ajax({})</ code> </ p>
     </ div>

展开原文

原文

Replace :

url: 'filter.views.myajax'

With

url: '{% url filter.views.myajax %}'

# **For Django >= 1.5 **

url: '{% url "filter.views.myajax" %}'

inside $.ajax({})

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐