我正在嘗試使用AJAX調(diào)用將數(shù)據(jù)發(fā)送到我的django視圖,然后我希望在另一個(gè)視圖中使用它。當(dāng)用戶點(diǎn)擊一個(gè)特定的詞時(shí),known_words必須加一個(gè)(這部分工作)。但我也想知道用戶點(diǎn)擊了哪個(gè)詞(我可以在模板中訪問這個(gè):{{item.0}}。這是我無法開始工作的部分。我的html的相關(guān)部分(這是我表的最后一列,第一列包含{{item.0}}):<a href="javascript:" class="word_known btn btn-warning btn-sm" data-word="{{item.0}}" data-songpk="{{song_pk}}" data-userpk="{{user_pk}}">Yes</a>我的js:$(document).ready(function() { var known_words = 0; var clicked_words = []; $(".word_known").click(function() { known_words++; var reference = this; var songpk = $(this).data('songpk'); var userpk = $(this).data('userpk'); var clicked_words = $(this).data('clicked_words'); //I know this part is wrong, how can I append the word to the list? $.ajax({ url: "/videos/songs/vocab/known/"+songpk+"/"+userpk+"/", data: {known_words: known_words, clicked_words: clicked_words}, success: function(result) { $(reference).removeClass("btn-warning"); $(reference).addClass("btn-success"); $(reference).text("Known"); }, failure: function(data) { alert("There is an error!") } })});})意見:def word_known(request, pk_song, pk_user): if request.method =='POST': pass elif request.method == 'GET': known_words = request.GET.get('known_words', '') clicked_words = request.GET.get('clicked_words', '') request.session['known_words'] = known_words clicked_words = [] clicked_words.append(request.session['clicked_words']) print('The number of known words is {} and clicked words are {}'.format(known_words, clicked_words)) return HttpResponse(json.dumps(known_words))在控制臺(tái)中,當(dāng)我單擊一個(gè)單詞(不是“你好”)時(shí),我會(huì)在控制臺(tái)中得到以下信息:The number of known words is 1 and clicked words are ['hello']如果我第二次點(diǎn)擊另一個(gè)詞:The number of known words is 2 and clicked words are ['hello']所以計(jì)數(shù)器正在工作,但不是單詞列表。我該如何解決?
Ajax 調(diào)用以獲取單擊的對(duì)象列表
開心每一天1111
2022-11-11 14:43:34