Problems with updating the ajax page

Asked

Viewed 68 times

0

Good afternoon, my friends! I’m skating and I don’t move, the structure of the code is as follows:

insert.php

$.ajax({
   type: 'POST',
   url: 'action.anuncio.php',
   data: dados,
   dataType: 'json',
   success: function(response) {
            location.reload();
      }
});

action.anuncio.php

include_once 'class.anuncio.php';
$anuncio = new Anuncio();
switch ($_POST[]) {
    case 1:
       $anuncio->inserir('informações');
       break;


}

class.anuncio.php

include_once 'conexao.php';
public Anuncio() {
    function listarAnuncios() {}
    function inserir(){
           /*instruçoes sql para gravação*/
           if($result) {
                $response = array('sucess'=>sucess);
                encode_json($response);
            }
      }


}

The code records and deletes only that it does not update the page, I need to press F5 to update, it seems that the ajax trigger is not receiving the correct answer to give the Reload.

  • 1

    When you specify dataType: 'json',, the server response must be a response with a valid JSON in the body and a header Content-type: application/json. How’s the php return? did you set the header? a suggestion: if you don’t want any answers back (you do Reload), you don’t have to wait for a JSON. Just send a code 200 in case of success and a code 403 for example in case of error. Another tip, if you allow me: check the tab Network of Developer Tool (Developer Tool) and try to locate this request and what it is returning, if it is expected.

1 answer

0

The function Success is not being called. The truth is that many reasons can cause this, you have, for example, put a console.log(dados) so that we can see what is being passed as JSON?

Another tip is to replace the code in action.anuncio.php by removing the SWITCH as follows:

if(isset($_POST['chave'])) $anuncio->inserir('informações'); 

Thus preventing errors of Undefined on the PHP side.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.