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.
When you specify
dataType: 'json',
, the server response must be a response with a valid JSON in the body and a headerContent-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.– mrlew