How do I get json response in javascript / jquery?

2

I have the link:

https://dominio.com/apiJSON.php?data={"login":"[email protected]","senha":"Minhasenha","campanha":"ID 1234","mensagens":{"1":{"numero":2799999999,"msg":"Uma mensagem qualquer","data":"2015-10-19 01:07:52"}}}

By pasting this link in the browser with the correct parameters I get the following response in the browser:

{"tip":1,"msg":[{"numero":2799999999,"id":"1234","status":1}]}

I want to get this answer and work it in javascript or jquery. I need to work on this information. Can someone help me? Thanks!

Note: I asked this question and I got it in php. But I need to work on it in another language by which php the server barred.

    
asked by anonymous 23.05.2016 / 13:56

2 answers

0

Thiago, I find it a bit strange to pass a json as a string to an API, especially by GET.

But you can consume this URL in two ways: AJAX or JSONP:

AJAX

var json = {
    "login":"[email protected]",
    "senha":"Minhasenha",
    "campanha":"ID 1234",
    "mensagens":{
        "1":{
            "numero":2799999999,
            "msg":"Uma mensagem qualquer",
            "data":new Date(2015, 9, 19, 1, 7, 52)
        }
    }
};

var data = new FormData();
data.append("data", JSON.stringify(json)):

var httpRequest = new XMLHttpRequest();
httpRequest.open("GET", "https://dominio.com/apiJSON.php", true);
httpRequest.responseType = "json";
httpRequest.addEventListener("readystatechange", function (event) {
    if (httpRequest.readyState == 4) {
        if (httpRequest.status == 200) {
            console.log(httpRequest.response): //seu objeto JSON;
            /* Faça alguma coisa com o "httpRequest.response" */
        } else {
            console.log("Ocorreu um Erro");
        }       
    }
})
httpRequest.send(data);

JSONP

Note that I have added one more argument in your URL, callback=minhaFuncao , the callback parameter defines the name of the function that will receive the json object obtained as a return of the request.

var json = {
    "login":"[email protected]",
    "senha":"Minhasenha",
    "campanha":"ID 1234",
    "mensagens":{
        "1":{
            "numero":2799999999,
            "msg":"Uma mensagem qualquer",
            "data": new Date(2015, 9, 19, 1, 7, 52)
        }
    }
};  

var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://dominio.com/apiJSON.php?data=" + JSON.stringify(json) + "&callback=minhaFuncao";
var minhaFuncao = function(data) {  
    /* data é o objeto JSON retornado pelo script acima */
}
document.head.appendChild(script);
    
23.05.2016 / 14:14
1

Make an ajax call I believe is the best way, given that your javascript question is going to post accordingly, keep in mind that with jquery or other libs you could greatly reduce this code:

var xmlhttp;
var data;

if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp = new XMLHttpRequest();
} else {
    // code for IE6, IE5
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == XMLHttpRequest.DONE ) {
       if(xmlhttp.status == 200){
           data = xmlhttp.responseText;
       }
       else if(xmlhttp.status == 400) {
           alert('There was an error 400')
       }
       else {
           alert('something else other than 200 was returned')
       }
    }
};

xmlhttp.open("GET", 'https://dominio.com/apiJSON.php?data={"login":"[email protected]","senha":"Minhasenha","campanha":"ID 1234","mensagens":{"1":{"numero":2799999999,"msg":"Uma mensagem qualquer","data":"2015-10-19 01:07:52"}}}', true);
xmlhttp.send();

// agora vamos tranformar a string JSON num objeto de javascript caso o pedido tenha sido bem sucedido:
if (typeof data != 'undefined') {
    data = JSON.parse(data);
}
console.log(data);
    
23.05.2016 / 14:07