avaaren

хЕЛП

Mar 15th, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. ЕСТЬ ВОТ ТАКАЯ ФОРМОЧКА
  2.  
  3. <form action="" method="post" id="post-form">
  4. {% for answer in answers %}
  5. <input type="radio" id="{{ answer.pk }}" name="answer" value="{{ answer.answer_text }}">
  6. <label for="{{ answer.pk }}">{{ answer.answer_text }}</label><br>
  7. {% endfor %}
  8. <button type="submit" class='button'>Next question</button>
  9. {% csrf_token %}
  10. </form>
  11.  
  12. ЕСТЬ ВОТ ТАКОЙ АЖАКС
  13. $(document).on('submit', '#post-form',function(e){
  14. let id = $('input[type=radio][name=answer]:checked').attr('id');
  15. console.log();
  16. // Тут мы делаем +1 к куррент квещну и показываем его
  17. alert(id);
  18. $.ajax({
  19. url : "{% url 'game:quiz_detail' 1 %}", // the endpoint
  20. type : "POST", // http method
  21. data : { the_post : 'sskdakdas' }, // data sent with the post request
  22.  
  23. // handle a successful response
  24. success : function(json) { // remove the value from the input
  25. console.log(json); // log the returned json to the console
  26. console.log("success"); // another sanity check
  27. },
  28.  
  29. // handle a non-successful response
  30. error : function(xhr,errmsg,err) {
  31. $('#results').html("<div class='alert-box alert radius' data-alert>Oops! We have encountered an error: "+errmsg+
  32. " <a href='#' class='close'>&times;</a></div>"); // add the error to the dom
  33. console.log(xhr.status + ": " + xhr.responseText); // provide a bit more info about the error to the console
  34. }
  35. });
  36. И ТАКОЙ КУСОК ВЬЮХИ
  37. ПОЧЕМУ В ЗАПРОС НЕ ПОПАДАЕТ ВОТ ЭТО data : { the_post : 'sskdakdas' }, // data sent with the post request И ВООБЩЕ
  38. КАК БУДТО НЕ ОТПРАВЛЯЕТСЯ АЖАКС ЗАПРОС, А ОТПРАЛВЯЕТСЯ ОБЫЧНЫЙ ПОСТ ЗАПРОС С ФОРМЫ??
  39. if request.method == 'POST':
  40. response_data = {}
  41. print(request.body)
  42. s = request.POST.get('the_post')
  43. response_data['su'] = '2ddf'
  44. response_data['dasdas'] = s
  45. return HttpResponse(
  46. json.dumps(response_data),
  47. content_type="application/json"
  48. )
Add Comment
Please, Sign In to add comment