0
I am working on the FLUIG (TOTVS) tool, and I want to read a JSON that is delivered via REST. Here is a photo of the URL (intranet):
Contents:
{"content":{"Matrícula_RH":"10555","UserDocLanguage":"pt_BR","UserEmailHTML":"true","UserProjects":"","UserQuotaDocument":"500","UserSpecialization":"","UserWorkflowGroup":"TI","UX-APP-COMPANY":"88303375000171_0240050312_RS","WCMUserLang":"pt_BR"},"message":{"message":"OK","detail":"OK","type":"INFO","errorCode":null}}
JSON reading code:
function executa() {
var url = "http://10.0.0.1:8181/api/public/2.0/users/listData/teste";
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200){
var json = JSON.parse(xmlhttp.responseText);
var contador = json.length;
alert(contador);
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
but always gives this error in the browser console(Chrome):
Uncaught SyntaxError: Unexpected token < in JSON at position 0
at JSON.parse (<anonymous>)
at XMLHttpRequest.xmlhttp.onreadystatechange (rest.html:17)
When I look, the wrong line is this:
var json = JSON.parse(xmlhttp.responseText);
Am I treating JSON wrong? I took this code from the internet and found it very generic, researched about this error and tested some changes without success. I am very beginner in javascript, if someone can explain to me why this does not work and which is the correct method I appreciate.
I tried a new code that worked just need to learn how to get the JSON content:
function executa_new() {
$.ajax({
type: 'GET',
url: 'http://10.0.0.1:8181/api/public/2.0/users/listData/teste',
dataType: 'jsonp',
success: function() {
console.info("foi");
}, error: function(e){
alert('Ocorreu um erro durante a chamada ' + e);
}
});
}
Gives a
console.log(xmlhttp.responseText)
to see what’s in the output. with F12 -> console you can check the output. Something else, tryJSON.parse(xmlhttp.responseText.content)
– Marconi
It brings a completely different HTML page: https://pastebin.com/LQkCFtKz
– CarlosM
see the file you call by ajax, the problem may be in it q is calling something that returns information before the time.
– Guilherme Golfetto