I have the following return in json:
{
"ConsultarRegistroPorCodigoResult": {
"Codigo": 2,
"CodigoSetor": 1,
"Login": "ednilson1",
"Nome": "Ednilson",
"RegistroAtivo": true,
"Senha": "123456",
"Tipo": "D"
}
}
<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><scripttype="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'
, so I saw json as invalid, as "convert" to a valid json?
The error occurs at line var myData = JSON.parse(result.d)
, the complete error message follows:
(index): 1 Uncaught SyntaxError: Unexpected token in JSON at position 0 at JSON.parse () at Object.success ((index): 31) at 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)
return of variable result :
Object {CheckRegistrationCodeResult: Object}