2
I would like to know what is wrong with my job, because in allocating obj = data
, the same sage object object
, the date value is a JSON returned by the WEBSERVICE.
Date value:
[{"descricao":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. In tristique \nligula sed nibh finibus, id placerat elit mollis. Proin dictum sed felis vitae vulputate. Nunc vitae velit feugiat, porttitor \nfelis quis, pharetra risus. Integer id dolor sapien. Aenean ultricies, risus at lacinia blandit, leo mi tincidunt risus, \nat gravida nisi nisl ut dui. Donec vitae consectetur urna. Phasellus at augue vel nisl semper aliquet eu vel ante. Ut venenatis \nlacus id velit aliquet, ac congue neque mollis. Quisque at elit mollis arcu condimentum imperdiet.","idEvento":3,"inicio":"05/10","palestrante":"Profº José Marinho","qtdVagas":40,"termino":"09/10","titulo":"Palestra de Jogos"}]
Javascript function:
$(document).ready(function(){
$(".btnSubmit").click(function(evt) {
evt.preventDefault();
$.ajax({
type: "GET",
url: "http://localhost:8080/SemanaEngenharia/webresources/services.evento",
data: "", /* redundante */
dataType: "text",
success: function(data) {
alert(data);
obj = JSON.parse(data); /* nao funciona*/
//$("#p").html(obj.titulo);
alert(obj);
}
}); // fim ajax
});
});
In doing obj.titulo
(one of the JSON fields), the return is "Undefined".
Have you tried to define the
dataType
as JSON to not need to do the conversion manually?– Woss
If there are brackets it is because it is converting one array of objects. If you do not need the array, convert the object directly. Without seeing the code behind all this it is impossible to say.
– Woss
I found the bug. On my webservice, it returns a list of all events, so the brackets. How could I change this? I’m using JPA
– Weslley Fillipe
Search in the documentation what the
dataType
makes you understand.– Woss
my method in java: https://imgur.com/a/7MYuQcT it returns a list, so the brackets and why parse json is not working. Would have some possibility to change the structure of this JPA without using list?
– Weslley Fillipe
In case you change Java to return a different structure, I suggest that ask another question specifically about this (just be sure to do a search before to see if this question already exists on the site).
– hkotsubo