1

I'm trying to get the return of the request in the url, with javascript, but I'm having problems.

I have the follow code:

Controller:

@GetMapping("/linguagens")
@ResponseBody
public Map<String, Linguagem> linguagens(){
    Map<String, Linguagem> linguagens = new HashMap<>();
    linguagens.put("linguagem", new Linguagem("Java", true));
    return linguagens;
}

HTML page:

            <button id="verLinguagens">Ver linguagens</button>
        <div id="linguagens">

        </div>

JavaScript code:

$('#verLinguagens').click(function (e) {
e.preventDefault();
$.get("/linguagens")
    .done(function (data) {
        $('#linguagens').html("Linguagens: " + data.linguagem)
        console.log(data.linguagem)
        console.log(data)
    });

});

In my page show the result, but show like this:

Linguagens: [object Object]

how can I show the correct result? like this

{
"Linguagem": {
    "nome": "Java",
    "compilada": true
}

}

Thanks!

3 Answers 3

1

It works!!

Linguagens: {"nome":"Java","compilada":true}

but, like this, I can put each value of key in some label? like, value "java" in label with id "javaLabel"?

Sign up to request clarification or add additional context in comments.

Comments

0

You can use JSON.stringify(data) to convert the data to a json representation.

Comments

0

Would this give you what you're after?

var data = {
    "Linguagem": {
        "nome": "Java",
        "compilada": true
    }
};

console.dir(data);
var pretty = JSON.stringify(data, null, '\t');
document.getElementById('content').innerHTML = pretty;
  
<pre id="content"></pre>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.