0

我试图通过 AJAX/JSON 发布 POST,但服务器报告 HTTP 500 错误。

看法

url(r'^digajax$', 'digajax.views.main'),
url(r'^digajax_ajax$', 'digajax.views.ajax'),

模板

<!DOCTYPE html>
<html lang="en">
 <head>
    <meta charset="utf-8">
</head>
<body>
    <script type='text/javascript' src='http://code.jquery.com/jquery-1.8.2.js'></script>

<script type="text/javascript">
        $(document).ready(function() {
                $("#button").click(function() {
                        var input_string = $("#digInput").val();
                        $.ajax({
                                url : "/digajax_ajax",
                                type : "POST",
                                data : {
                                        clientinput : input_string,
                                        csrfmiddlewaretoken: '{{ csrf_token }}'
                                },
                                success : function(data) {
                                        jQuery('#result').html(data).hide().fadeIn(1500);
                                },
                                error : function(xhr,errmsg,err) {
                                        alert(xhr.status + ": " + xhr.responseText);
                                }
                        });
                        return false;
                });
        });
</script>


                <form method="post" name="diginput form" action="/digajax_ajax">
                {% csrf_token %}
                                <input name="digInput" id="digInput" type="text">
                                <input id="button" type="button" value="lookup"></input>
                </form>

<p id="result">
</p>

</body>
</html>

任何想法,firebug 输出只显示 500 HTTP 错误。谢谢,

4

1 回答 1

0

在更改 jquery $.ajax 并从实际表单中删除帖子后,它现在可以工作了。

<html lang="en">
 <head>
    <meta charset="utf-8">
</head>
<body>
    <script type='text/javascript' src='http://code.jquery.com/jquery-1.8.2.js'></script>

<script type="text/javascript">
        $(document).ready(function() {
                $("#button").click(function() {
                        var input_string = $("#digInput").val();
                        $.ajax({
                                url : "/digajax_ajax",
                                type : "POST",
                                dataType: "json",
                                data : {
                                        clientinput : input_string,
                                        csrfmiddlewaretoken: '{{ csrf_token }}'
                                },
                                success : function(json) {
                                        $('#result').append( 'Response Value: ' + json.clientinput1);
                                },
                                error : function(xhr,errmsg,err) {
                                        alert(xhr.status + ": " + xhr.responseText);
                                }
                        });
                        return false;
                });
        });
</script>


                <form method="post" name="diginput form" >
                {% csrf_token %}
                                <input name="digInput" id="digInput" type="text">
                                <input id="button" type="button" value="lookup"></input>
                </form>

<div id="result">
</div>

</body>
</html>
于 2013-03-14T07:52:09.077 回答