Ajax problems in PHP MVC project

0

I'm having trouble using ajax with html.

I have an html file that answers questions from a chat and I put the ajax below ajax. It redirects to the controller questions and the answer method. The request status is 200, but nothing happens. I have debugged and seen that ajax does not access the method, it does not pass any of the variables that I need.

$(document).ready(function(){

  $(".form-response").on('click',function(e){

    e.preventDefault();
    var seller_id = $(this).attr("seller_id");
    var dataValue = $(this).attr("data-value");
    var resposta = $("#resposta-pergunta-" + dataValue).val();
    console.log(dataValue);
    console.log(resposta);
    console.log(seller_id);

    $.ajax({
      method: 'POST',
      url : '../perguntas/responder',
      data: {
        id: dataValue,
        resposta: resposta,
        seller_id: seller_id

      }}).done(function(data){

        console.log($("#card-" + dataValue).hide());
        alert("Respondido com Sucesso! - Ajax");

      }).fail(function(){

        alert('Problema para RESPONDER');

      });
    return false;   });

When I click the answer button, the information appears in my console.

My project is in MVC and I'm using twig to render the templates. I have a layout.html file, which has the header and another file called form.html, which would be between the {% block form%} and {% endblock%}. My ajax is along with the form.html

    
asked by anonymous 05.07.2018 / 02:28

3 answers

0

Have you checked if the click event is triggered? Try printing some static text below the click event to see if it's triggering the event.

$(".form-response").on('click',function(e){
 console.log('Está acionando o evento');
 ...
    
05.07.2018 / 17:35
0

At this point:

url : '../perguntas/responder',

should be the "file.ext" ex: '../perguntas/responder.php' , which contains the method that treats your request (for example data: {...} ) and returns the result in success: function(data) .

I hope I have helped.

    
06.07.2018 / 01:04
-1

Check the Id or Name with care in your HTML and confirm that they are the same in the file and try to use this syntax in your controller receive json :

   jQuery.ajax({
        type: "POST",
        url: "../perguntas/responder",
        dataType: "json",
        data: {
             id: dataValue,
        resposta: resposta,
        seller_id: seller_id
},
        success: function(data)
        {    
            alert("sucesso");
        },
        error: function (request, status, erro) {

                alert("erro");                                      

             },
                 complete: function (jqXHR, textStatus) {
                //colocar aqui algo que deseja que faça ao terminar todo o processo (finnaly)
              }
       });
    
05.07.2018 / 18:36