Ajax Request and JS File Registration - Wordpress

0

Good morning,

I am setting up an ajax request to send to the source functions.php, I read in the wordpress documentation that the js file that handles the request and other things needs to be done. But when I send the request to the functions.php and nothing happens, can you help me with what I'm missing?

Ajax request:

 var Json =  JSON.stringify(contato,null,2);
//var formData = Json;

$.ajax({
    type: 'POST',
    url:ajax_url,
   // url:ajax_carrinho, 
    //url:'../grava-dados.php', 
    //data: contato,
    //data: Json,
    //data:{ action: 'gravaDadosContato',dados: contato},
    data:{ action: 'gravaDadosContato',dados: Json},
    //data: 'action=gravaDadosContato&'+formData
    //action: 'gravaDadosContato',
    //processData: false,
    //contentType: false,
    success: function (data) {
        alert(data);
    }
}).done(function( msg ) {
        alert( "Dados enviados com sucesso");
        location.reload();
});

functions.php

// adcionando o jquery
function register_jquery() {
    wp_enqueue_script('jquery');
}

add_action('wp_enqueue_scripts', 'register_jquery');

//registrando o arquivo carrinho.js
function register_carrinho() {
    wp_register_script(
    'carrinho',
    get_stylesheet_directory_uri() . '/js/carrinho.js',
    array('jquery'),
            true
);

wp_enqueue_script('carrinho');

}

add_action('wp_enqueue_scripts', 'register_carrinho');


add_action( 'wp_ajax_gravaDadosContato', 'gravaDadosContato' );
add_action( 'wp_ajax_nopriv_gravaDadosContato', 'gravaDadosContato' );


function gravaDadosContato(){
    global $wpdb;

$dados = json_decode($json);

//var_dump($dados);

$empresa    = $dados->{'empresa'};
$responsavel   = $dados->{'responsavel'};

//$dados = json_decode('dados', true);


return $empresa;
die;
}
    
asked by anonymous 17.08.2016 / 14:09

1 answer

0
  • Verify that the url of ajax_url is correct.
  • Your Json data is incorrect: Uncaught ReferenceError: Json is not defined

    • correct would be
      var x = { action: 'gravaDadosContato', dados: 'Json'};
      
       var x = { action: 'gravaDadosContato',dados: Json};
      console.log(x);
  • Make sure the request is arriving on your server.

    • in php echo 'chegou';
    • analyze the network of chrome. Check if the status is even returning 200 (success). What if the return is being json .
  • Sampleajaxrequest:moreinformationat link

     $.ajax({
       url: "http://meuservidor.com/salvar",
       type: "post",
       data: { nome: 'joao' },
       beforeSend: function () {
          console.log('antes de fazer a requisição');
       },
       error: function (e) {
          console.log('erro ao fazer a requisição'); // quando o servidor retorna status 400, 404, 500, ...
       },
       success: function (response) {
          console.log('sucesso ao fazer a requisição'); // quando o servidor retorna status 200, 201, ....
       }
     });
  • Since execution can reach $dados = json_decode($json); . You just have to implement your regrade negócio in php. From that point your concern should be with your% return return $empresa; it is in json format. I think not, a return json_encode($empresa); is missing. Also another concern should be with the return header: header('Content-Type: application/json'); .

  • 17.08.2016 / 17:31