机构列表页面调用ajax失败

来源:7-7 modelform提交我要学习咨询1

余泽锋

2017-10-01

提示错误:

Method Not Allowed (POST): /org/add_ask/

[01/Oct/2017 18:49:56] "POST /org/add_ask/ HTTP/1.1" 405 0

源码

#urls
from django.conf.urls import url, include
from .views import OrgView, AddUserAskView
app_name = 'org'
urlpatterns = [
    url(r'^list/$', OrgView.as_view(), name='org_list'),
    url(r'^add_ask/$', AddUserAskView.as_view(), name='add_ask'),
]

#views
class AddUserAskView(View):
    def POST(self, request):
        userask_form = UserAskForm(request.POST)
        if userask_form.is_valid():
            userask = userask_form.save(commit=True)
            return HttpResponse("{'status': 'success'}", content_type='application/json')
        else:
            return HttpResponse("{'status': 'fail', 'msg': '添加出错'}", content_type='application/json')

#templates
...
<div class="right companyright">
         <div class="head">我要学习</div>
         <form class="rightform" id="jsStayForm">
            <div>
                    <img src="{% static 'images/rightform1.png' %}"/>
                    <input type="text" name="name" id="companyName" placeholder="名字" maxlength="25" />
                </div>
            <div>
                    <img src="{% static 'images/rightform2.png' %}"/>
                    <input type="text" name="mobile" id="companyMobile" placeholder="联系电话"/>
                </div>
            <div>
                    <img src="{% static 'images/rightform3.png' %}"/>
                    <input type="text" name="course_name" id="companyAddress" placeholder="课程名" maxlength="50" />
                </div>
                <p class="error company-tips" id="jsCompanyTips"></p>
            <input class="btn" type="button" id="jsStayBtn" value="立即咨询 >" />
                {% csrf_token %}
            </form>
      </div>

      <div class="right companyrank layout">
         <div class="head">授课机构排名</div>

                {% for current_org in hot_orgs %}
                    <dl class="des">
                        <dt class="num fl">{{ forloop.counter }}</dt>
                        <dd>
                            <a href="/company/{{ current_org.id }}/"><h1>{{ current_org.name }}</h1></a>
                            <p>{{ current_org.address }}</p>
                        </dd>
                    </dl>
                {% endfor %}
      </div>
        </div>
</section>
{% endblock %}
{% block custom_js %}
<script>
    $(function(){
        $('#jsStayBtn').on('click', function(){
            $.ajax({
                cache: false,
                type: "POST",
                url:"{% url "org:add_ask" %}",
                data:$('#jsStayForm').serialize(),
                async: true,
                success: function(data) {
                    if(data.status == 'success'){
                        $('#jsStayForm')[0].reset();
                        alert("提交成功")
                    }else if(data.status == 'fail'){
                        $('#jsCompanyTips').html(data.msg)
                    }
                },
            });
        });
    })

</script>
{% endblock %}


写回答

1回答

佐仓千代

2017-10-02

题主我没有遇到你出现的

Method Not Allowed (POST): /org/add_ask/

[01/Oct/2017 18:49:56] "POST /org/add_ask/ HTTP/1.1" 405 0

这个错误

但是我出现了前端没有接收到status和msg的值的错误

因为我开始和你一样json语句是这样写的

            return HttpResponse("{'status': 'success'}", content_type='application/json')

            return HttpResponse("{'status': 'fail', 'msg': '添加出错'}", content_type='application/json')

老师视频里开始也是这样写的,后来他视频中的代码修改为了下面这样,但是不知道为什么他没有说出来

            return HttpResponse('{"status":"success"}', content_type='application/json')

            return HttpResponse('{"status":"fail", "msg":"添加出错"}', content_type='application/json')

这里这样写之后就正常了

更改了引号,应该是json格式的问题.

2
5
余泽锋
回复
佐仓千代
这头像看起来像程序媛,哈哈哈
2017-10-04
共5条回复

Python3.6+django+xadmin,打造在线教育平台

【毕设】Python 2.7到3.6 完美适配,Django升级2.0

3677 学习 · 4038 问题

查看课程