I'm 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):
Content:
{"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 it 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 line with error is this:
var json = JSON.parse(xmlhttp.responseText);
Am I handling JSON in the wrong way? I got this code on the internet and found it very generic, I researched this error and tested some changes without success. I'm a beginner in the javascript, if someone can explain to me why this does not work and which method is right, I appreciate it.
I tried a new code that worked just need to learn how to get the contents of JSON:
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);
}
});
}