小编dee*_*aws的帖子

数据未使用jquery .ajax()发送到Django视图

我正在尝试创建一个单独的ajax调用.ajax调用被发送到服务器,但是我发送的数据在视图中的请求对象中不可用.当我打印时request.post,它给出了<QueryDict: {}>.没有数据发送到服务器.我知道浏览器正在发送数据,因为我可以在chrome中的请求有效负载中查看它.

脚本:

$("#chatform").submit(function(e) {
    e.preventDefault();
    //serialText = $(this).serialize();
    var userText = $("#usertext").val();
    var xmlRequest = $.ajax({
            type: "POST",
            url: "/sendmessage/",
            data: {'tosend': userText},

            //dataType: 'json',
            contentType: "application/json; charset=utf-8",
            success: function(data){
                appendMessageSent(data.messagesent);
            }
    });
});
Run Code Online (Sandbox Code Playgroud)

view.py:

def send_message(request):
    if request.is_ajax():
        message = "The hell with the world"

        print request.POST
        json = simplejson.dumps(
            {'messagesent' : request.POST['tosend']+"This is how we do it"}
        )
        return HttpResponse(json, mimetype='application/javascript')
Run Code Online (Sandbox Code Playgroud)

HTML

<form id="chatform" action="" method="POST" >
        <input type='hidden' name='csrfmiddlewaretoken' value='8idtqZb4Ovy6eshUtrAiYwtUBboW0PpZ' />
        <input …
Run Code Online (Sandbox Code Playgroud)

django jquery django-views

2
推荐指数
1
解决办法
2590
查看次数

标签 统计

django ×1

django-views ×1

jquery ×1