javascript response does not appear on the page, only on the console

Asked

Viewed 330 times

0

Good night,

I’m using codeigniter, I’m implementing the pagseguro api, and I’m using a javascript to set the session id and call the payment methods, so far it’s all working, only in one function listMeiosPag() in javascript, it brings the result only in console.log, if I have it show the result in a page div, it simply does not show, nor does Alert display tb, but if I put Alert right in the page script it displays, I do not know how to solve it, if anyone can help me thank.

My javascript is like this:

   var pg = function (){


             var setSessionId = function(){

                  $.ajax({
                     url: 'http://[::1]/teste/pg_session_id',
                     dataType: 'json',
                     success: function(res){

                        if (res.erro == 0) {
                           var id_sessao = res.id_sessao;
                           PagSeguroDirectPayment.setSessionId(id_sessao);
                        } else {
                           alert('Erro: '+ res.erro +' '+res.msg);
                        }
                     }

                  });
             }



                   //Mostrar Métodos do pagamento
                   function listarMeiosPag(){

                      $('.btn-pagamento').on('click', function(e){

                         PagSeguroDirectPayment.getPaymentMethods({
                         amount: 500.00,
                         sucess:function(res){
                            if (res.erro == 0) {

                              $.each(res.paymentMethods.CREDIT_CARD.options, function(i, obj){
                               console.log(res); //aqui exibe a respota certa
                           $('meio-pag').append("<span>"+ obj.name +"</span>"); </div>');
                          //na div da pagina fica em branco a resposta
                            });

                      /*   console.log(res);*/
                            } else {
                               alert('Erro: '+ res.erro +' '+res.msg);
                            }
                         },
                         error: function(res){
                          console.log(res);
                         }
                      });
                   });
                }



             return {
                 init:function(){
                   listarMeiosPag()
                  setSessionId();
                 }
             }

          }();

          jQuery(document).ready(function(){
             pg.init();
          });   

My page looks like this:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!--Chama o  jQuery -->
<script src="<?php echo base_url('public/js/pagamentos.js') ?>"></script> 

//aqui é o botão que chama os métodos de pagamento
<button class="btn btn-primary btn-pagamento"> Pagar </button>

//aqui era a div que ele deveria exibir a resposta
 <div class="meio-pag"> </div>

The function listMeiosPag, should show the result in the div, but it does not work, and I do not know the pq, if you can help me thank.

  • 1

    the class js selector is ., he was missing $('.meio-pag')

  • shows console.log(res); pq is using to mount html obj.name? shouldn’t be res.name? I saw that the "obj" is in your each, but look what is in the "obj" before, debug there and will discover the problem

  • @Marceloboni put the . but it didn’t work yet, I tried to put an Alert and nothing tb.

2 answers

1

If the

console.log(res);

Displays the right answer, "res" is the answer, so:

$('meio-pag').append("<span>"+ res +"</span>"); </div>');

Just watch out for the possibility of res be an object and need the property, type, res.qualquerCoisa

  • I put it like this, but it didn’t work, it doesn’t display the answer in div. I don’t know why.

  • $('half-pag') already corrected that? if it is a $(". half-pag") class or if it is id $("#half-pag") . or #

  • Yes, I checked everything, but it was not, unfortunately...I will keep trying, Valew for help friend

1


Hi @Ragnar

I was seeing that you are assigning in the function a parameter "res", it is an object? If yes, where is being called?

Within the structure of its code, the res is behaving like an object that has attributes, check whether this function is treating correctly or whether the parameter received by the function is actually receiving an object with the properties it is trying to access.

res = { 
erro: "algum erro",
msg: "alguma mensagem de erro
 }

If res.error gets string, then in comparison if(res.erro == 0) should be some other validation as if(res.erro == "") which would indicate that the error attribute is empty. If it is to count, some get method should be used.

function(res){

                        if (res.erro == 0) {
                           var id_sessao = res.id_sessao;
                           PagSeguroDirectPayment.setSessionId(id_sessao);
                        } else {
                           alert('Erro: '+ res.erro +' '+res.msg);
                        }
                     }

Make sure you are accessing the properties correctly. This parameter apparently seems sufficient for what you are needing.

if (res.erro == 0) {

   $.each(res.paymentMethods.CREDIT_CARD.options, function(i, obj){
         console.log(res); //aqui exibe a respota certa
         $('meio-pag').append("<span>"+ obj.name +"</span>"); </div>');
         //na div da pagina fica em branco a resposta
   });

You could try putting $('meio-pag').append("<span>"+ res.PROPRIEDADE.name +"</span>"); </div>');

  • Junio I put the if (res.erro == "") { $.each(res.paymentMethods.CREDIT_CARD.options, function(i, obj){&#xA; console.log(res);&#xA; $('meio-pag').append("<span>"+ res.obj.name +"</span>"); </div>');&#xA; });} and yet it has not appeared in div...

  • @Ragnar From what I understood from the above code, obj is a parameter being passed to Function, so maybe it cannot be treated as property of the variable res. Try to print only the res without calling any attribute, since the console is already showing the correct answer. Note that if you are appearing on the console, it means you are entering the desired condition.

  • 1

    it was this msm, I was not knowing call him to show on screen, but now it worked, mto thanks for the help, sorry for the stupid kkk Valew brother

  • Please! I’m glad you were able to help! : ) Tmj

Browser other questions tagged

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