ajax提交没有达到内部端点

时间:2016-04-04 16:14:15

标签: javascript jquery python ajax django

我只是设置一个端点的ajax调用,就像我之前工作时一样,但是我得到500错误:

views.py:

@csrf_exempt
def save_file_ajax_endpoint(request):
    print('POST to ajax')
    text = request.POST['text']
    filepath = request.POST['filepath']
    print(filepath)
    print('yo')
    write_content(filepath, text)
    return redirect('index:index')

urls.py:

from django.conf.urls import patterns, include, url

from index import views

urlpatterns = patterns('',            
    url(r'^$', views.index, name='index'),
    url(r'^save/$', views.save_file_ajax_endpoint,  name='save-file'),
)

的index.html:

{% extends 'base.html' %}

{% load html_process %}

{% block body_block %}

    <div id="elephant" style="height: 500px; width: 100%;">{{ content }}</div>
    <br>
    <button id="save-button" class="btn btn-primary btn-sm new-email-button">Save</button>

    <script>
        function determineMode(filepath) {
            var modeString = "ace/mode/" + filepath;
            return modeString;
        }
    </script>

    <script src="/media/ace-builds/src-noconflict/ace.js" type="text/javascript" charset="utf-8"></script>

    <script>
        var editor = ace.edit("elephant");
        editor.getSession().setMode(determineMode('sh'));
        editor.session.setNewLineMode("unix");
        editor.setTheme("ace/theme/monokai");
        editor.setFontSize(15);
    </script>

    <script>
        $('#save-button').click(function(){
            var textBox = $('#editor').text();
            var filepath = '{{ filepath }}';
            alert(filepath)
            var saveFileEndpoint = {% url 'index:save-file' %};
            $.ajax({
                url: saveFileEndpoint,
                type: 'POST',
                dataType: 'html',
                data: {
                    filepath: filepath,
                    text: textBox
                },                
            });
            return false;
        });
    </script>



{% endblock %}

enter image description here

我点击保存得到500:

enter image description here

为什么这个按钮不发送ajax?谢谢

0 个答案:

没有答案