Return value per json and return to a function

Asked

Viewed 295 times

0

I have the following function:

function novasMensagens(Alerta) {
  var retorno = Alerta > 0 ?  Alerta : "";
  return retorno;
}

document.getElementById("msgNumero").innerHTML = novasMensagens();

Now I intend to return the value of Alerta of the above function by ajax of a query.

File php where I consult:

$query = "SELECT COUNT(A.Alertas) AS Alerta
FROM(
SELECT Id, De, Assunto, Conteudo, Prioridade, TIME_FORMAT(Recebido,'%h:%i') AS Hora, DATE(Recebido) AS Data,
Email, Tipo, centrodb.Alertas.Para, Status, Count(Status) AS Alertas FROM centrodb.Alertas LEFT OUTER JOIN centrodb.ValAlertas
ON centrodb.ValAlertas.IdSMS = centrodb.Alertas.Id AND centrodb.ValAlertas.Para = centrodb.Alertas.Para
WHERE centrodb.Alertas.Para = 'Pedro' 
Group BY Id, De, Assunto, Conteudo, Prioridade, Recebido, Email, Tipo, centrodb.Alertas.Para, Status) AS A
WHERE A.Alertas = '0'
LIMIT 10";  
      $result = mysqli_query($conn, $query);
      $row = mysqli_fetch_array($result);  
      echo json_encode($row);  

In the above function I added ajax as follows:

function novasMensagens(Alerta) {
  var retorno = Alerta > 0 ? Alerta : "";
  return retorno;

  $.ajax({
    type: 'GET',
    url : './fetchbusca',
    success : function(data) {
     console.log(json);
    }
});
}

document.getElementById("msgNumero").innerHTML = novasMensagens();

But nothing returns in json The value that will return on json, I have to assign it to Alerta, that is within the variable retorno.

2 answers

1


Your function has errors that are preventing it from running. First you have put a return before the call ajax, That will stop the function in that return and nothing below it will be executed. Second I yours callback you are expecting the date variable and on console.log is using the json variable. Make changes:

function novasMensagens(Alerta) {
    var retorno = Alerta > 0 ? Alerta : "";
    //return retorno;

    $.ajax({
        type: 'GET',
        url : './fetchbusca',
        success : function(data) {
            console.log(data);
        }
    });
}
  • was what I needed. Now I have only one doubt, the value that is returned within the Success function, is assigned above to the Alerta that is within the variable retorno?

  • Unless you do this assignment within Success, it will not be assigned

  • do it this way: var Alerta = data;

0

Hello, good afternoon!

Follow an example using a GET server request

In the function, you must replace the URL with the method path in your controller. (for example: /api/Messages/Verification)

Example:

OF

url : 'https://viacep.com.br/ws/31030430/json/'

FOR:

url : '/api/Messages/Check'

When the value search is successful, the function will write where Document.getElementById("msgNumero") is indicated. innerHTML.

This example of mine, I do a search for a zip code.

MY CASE Then, I verify that the cep I passed is of MG, if it is I return the ibge field, if it is not I return "0".

YOUR CASE In your case, you will look if the return is greater than 0, if it is because you have new messages and you then return the value of messages 1, 2, 5... and you will be saved this number in the Document.getElementById("msgNumero"). innerHTML.

function novasMensagens() {
  $.ajax({
    type: 'GET',
    url : 'https://viacep.com.br/ws/31030430/json/',
    success : function(data) {
    data.uf === "MG" ? document.getElementById("msgNumero").innerHTML = data.ibge :
    document.getElementById("msgNumero").html = "0";
    console.log(data.uf);
    console.log(data.ibge);
    console.log(data.cep);
    },
    error: function(){
      document.getElementById("msgNumero").html = "ERRO"
    },    
  });
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="msgNumero"></div>
<br />
<button class="btn btn-info" onclick="novasMensagens();">Verificar novas mensagens</button>

Note. I left three console-logs in the function to help you see the results that come.

You can change the zip code in the function to see what happens. The zip code of the example is 31030430

Browser other questions tagged

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