Change JSON Response in php

Asked

Viewed 55 times

-1

Hello friends I need to change the answer in json but I’m not getting it. follows:

    $ch       = curl_init('http://sams-api.arar.local/send');
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, 'phone_number=' . $phoneNumber . '&message=' . 'Ola sua Senha no Painel do Assinante da Mar Telecom e: '.$senha_gerada);
    $data = curl_exec($ch);


    echo($_SESSION['contrato_recupera']['CL_Sms']);
        die;


            $.ajax({ url: 'carregaDados.php',
                data: {function2call: 'buscaContrato', contrato: $("#contratos_vinculados").val()},
                type: 'post',
                success: function(output) 
                {
                    var dados = JSON.parse(output);

                //console.log(output);
                $("#celularRecupera").text(dados[0].telefone);

                }
            });

        });

          function recuperaSenha()
        {
            $("#recuperaBtn").hide();
            $.ajax({ url: 'carregaDados.php',
                data: {function2call: 'criaSenha'},
                type: 'post',
                success: function(output) 
                {

                alert('Senha enviada por SMS para '+output ); window.location.href = 'entrar.php';

the answer comes as follows::

            Senha enviada por SMS para {"error":false,"message":"Message sent","data":[]}22996058462

How do I get this {"error":false,"message":"Message sent","data":... ??? anyone help ?

  • Your php is mixed with javascript or are 2 different guys?

1 answer

-1

You can use a regular expression in an replace:

{.+}  // irá selecionar tudo que tiver entre as chaves (inclusive as chaves)

Example:

output = 'Senha enviada por SMS para {"error":false,"message":"Message sent","data":[]}22996058462';
output = output.replace(/{.+}/, '');
console.log(output);

Browser other questions tagged

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