weixin_33686714 2014-03-21 04:55 采纳率: 0%
浏览 35

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条回答

  • weixin_33734785 2014-03-21 04:59
    关注

    Replace :

    url: 'filter.views.myajax'
    

    With

    url: '{% url filter.views.myajax %}'
    
    # **For Django >= 1.5 **
    
    url: '{% url "filter.views.myajax" %}'
    

    inside $.ajax({})

    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮