JSON Treatment

0

Through JQUERY, I need to make a request via POST where I will get a JSON with a token. The JSON return I get after sending the POST is this:

{
    "Token": "e27bb0a7-e65b-4cc3-a82e-7a2a3c26a248",
    "Codigo": 0
}

My question is: How do I read this token? One analyst informed me that I should do something like this:

$(document).ready(function() {

	var settings = {
	  "async": true,
	  "crossDomain": true,
	  "url": "https://siteexemplo.br/login/geraTok",
	  "method": "POST",
	  "headers": {
	    "content-type": "application/json",
	  },
	  "data": {
	    "RA": "12345",
	    "senha": "xxx"
	  }
	}
	 
	$.ajax(settings).done(function (response) {
	  console.log(response);
	});

});

But after that? Where's the token? How do I pass it to a variable for example?

Thank you guys!

    
asked by anonymous 14.11.2017 / 11:55

2 answers

2
{ "Token": "e27bb0a7-e65b-4cc3-a82e-7a2a3c26a248", "Codigo": 0 } é a resposta do servidor em 'JSON'.

response takes this response in object form, so just store it ...

$.ajax(settings).done(function (response) {
          var token = response.Token;
          console.log(token);
        });
    
14.11.2017 / 12:11
0

The data return appeared in the response variable which is the function parameter when done, you can simply use the ponto notation to access the keys of a json , see:

$.ajax(settings).done(function (response) {
  console.log(response.Token); // pega o token retornado do json
  console.log(response.Codigo); // pega o código retornado do json
});
    
14.11.2017 / 12:11