1
I have the following return in json:
{
"ConsultarRegistroPorCodigoResult": {
"Codigo": 2,
"CodigoSetor": 1,
"Login": "ednilson1",
"Nome": "Ednilson",
"RegistroAtivo": true,
"Senha": "123456",
"Tipo": "D"
}
}
Of webservice done in WCF I am using the following code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<form id="form1">
<div class="jumbotron">
<input type="text" id="codUser"/>
<button onclick="ConsUsuario(); return false;">Consulta Usuario</button>
</div>
<div><table id="datagrid"></table></div>
</form>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
function ConsUsuario(){
var value = $("#codUser").val();
$.ajax({
type: "GET",
url: "ServiceRestPub/ServiceUsuario.svc/ConsultarRegistroPorCodigo/" + value,
contentType: "application/json",
dataType: "json",
success: function (result) {
debugger;
var tabela = $("#datagrid");
var rows = "";
tabela.find("tbody td").remove();
var myData = JSON.parse(result.d);
for (var i = 0; i < myData.length; i++) {
var obj = myData[i];
alert(obj.descricao);
rows += "<tr>";
rows += " <td>" + obj.id_usuario + "</td>";
rows += " <td>" + obj.nm_usuario + "</td>";
rows += " <td>" + obj.ds_login + "</td>";
rows += " <td> <input type='checkbox' /> </td>";
rows += "</tr>";
}
// tabela.find("tbody").html(rows);
tabela.html('<tbody>' + rows + '</tbody>');
//console.info(result.d);
}
});
}
</script>
Returns the error 'Unexpected token u in JSON at position 0'
, I saw that json is invalid, like "convert" to a valid json?
Error occurs on line var myData = JSON.parse(result.d)
, follow the full error message:
(index):1 Uncaught Syntaxerror: Unexpected token u in JSON at position 0 at JSON.parse () At Object.Success ((index):31) l (jquery.min.js:2) At object.fireWith [as resolveWith] (jquery.min.js:2) AT T (jquery.min.js:2) at Xmlhttprequest. r (jquery.min.js:2)
the return of the variable result:
Object {Query Texttexttextsummary: Object}
In which line does the error occur? in Rows?
– Aline
Put what’s coming in the variable
result
to help you better...– JuniorNunes
I edited and added what you suggested, obg
– Gleyson Silva
@Intelidersistemas The json return you have placed is from
result
orresult.d
? If it’s fromresult
take off the.d
ofJSON.parse
.– JuniorNunes
if I take . d the error changes
Uncaught SyntaxError: Unexpected token o in JSON at position 1
, error happens on the same line– Gleyson Silva
I would like to see obj: result, q already seems to be coming deserialized.
– Aline
var myData = JSON.parse(result. d); to: var myData = JSON.parse(result);
– Aline
@Aline change to what you indicated the error changes: Unexpected token o in JSON at position 1
– Gleyson Silva