第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Django div 重新加載 - POST 后的 ajax GET 未獲取最新的數(shù)據(jù)庫模型實(shí)例

Django div 重新加載 - POST 后的 ajax GET 未獲取最新的數(shù)據(jù)庫模型實(shí)例

暮色呼如 2021-08-24 15:20:40
在類似聊天的應(yīng)用程序中,我使用 ajax 調(diào)用來發(fā)布新消息并更新頁面上顯示的消息,而無需重新加載整個(gè)頁面。我的 ajax 發(fā)布工作調(diào)用 - 在數(shù)據(jù)庫中創(chuàng)建了一個(gè)新的消息實(shí)例。但是,之后當(dāng)我進(jìn)行 ajax 調(diào)用以獲取所有消息時(shí),結(jié)果查詢集中缺少新消息。如果我完全刷新頁面,我可以看到所有消息,但這不是我想要的。HTML 消息模板:    {% for message in messages %}    <p>        {{ message.content }}    </p>    {% endfor %}HTML 聊天模板:<div id="chat">    {% include "messages.html" %}</div><form id="post-message-form", method="post" enctype="multipart/form-data">    [my form goes here]</form>JavaScript:$('#post-message-form').on('submit', function(event){    event.preventDefault();    $form = $(this);    var data = new FormData($form.get(0));    $.ajax({        url: '/post/a/new/message/',        type: 'POST',        data: data,        success: refresh_chat,        cache: false,        contentType: false,        processData: false    })    return false;}function refresh_chat(){    $.ajax({        url: '/get/all/messages/,        type: 'GET',        success: function(json) {            $('#chat').html(json['data']);        }    })    return false;}意見:import jsonfrom django.template import loaderfrom .forms import MessageForm# /post/a/new/message/def post_message(request):    if request.method == 'POST':        form = MessageForm(request.POST)        if form.is_valid():            message = form.save()            return HttpResponse(                json.dumps({'status': 1,                            'message': 'Message posted!'}),                content_type='application/json'            )# /get/all/messages/def get_messages(request):    if request.method == 'GET':        messages = loader.render_to_string('messages.html', context={'messages': Message.objects.all(), 'form': MessageForm()})        return HttpResponse(            json.dumps({'data': messages}),            content_type='application/json'        )有什么想法為什么我在 POST 后調(diào)用 ajax GET 時(shí)沒有獲得最新的數(shù)據(jù)庫數(shù)據(jù)?謝謝!
查看完整描述

1 回答

?
子衿沉夜

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊

事實(shí)證明,如果我在 ajax“完成”調(diào)用而不是“成功”上進(jìn)行聊天刷新,它會(huì)起作用:


$('#post-message-form').on('submit', function(event){

    event.preventDefault();

    $form = $(this);

    var data = new FormData($form.get(0));


    $.ajax({

        url: '/post/a/new/message/',

        type: 'POST',

        data: data,

        cache: false,

        contentType: false,

        processData: false

    }).done(function() {

        refresh_chat();

    });


    return false;

}

感謝您的評(píng)論!


查看完整回答
反對(duì) 回復(fù) 2021-08-24
  • 1 回答
  • 0 關(guān)注
  • 257 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)