Advertisement
yesamarcos

Exemplo de Ajax chamando o controller e retornando json

Jul 7th, 2016
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Name:           Custom JS Scripts
  3. Theme Version:  1.0
  4. */
  5. jQuery(function($){
  6.  
  7.     /**
  8.      * [description]
  9.      * @param {} []
  10.      * @param {} []
  11.      */
  12.     $("#categoria-principal").on("change", function(){
  13.         $.ajax({
  14.             url: CAMINHO_JAVASCRIPT+'/buscar-subcategorias',
  15.             type: 'POST',
  16.             data: {id: $("#categoria-principal option:selected").val()},
  17.             dataType: 'json',
  18.             success: function(data){
  19.                 // Buscado os dados das subcategorias, processa-se o layout
  20.                 $('#categoria-secundaria').find('option').remove().end()
  21.                     .append('<option value="">Escolha uma das opções abaixo ...</option>');
  22.                
  23.                 $.each(data, function(chave, valor) {
  24.                     $('#categoria-secundaria').append($('<option>')
  25.                         .attr('value', valor.id).text(valor.atividade));
  26.                 });
  27.  
  28.             }
  29.  
  30.         });
  31.        
  32.     });
  33.  
  34.     /**
  35.      * [description]
  36.      * @param {}
  37.      * @param {}
  38.      */
  39.  
  40. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement