0

I have the following code

  function fncGetUsuarioProducao(p1) {
      $.ajax({
          type: "POST",
          url: "cadRecebimentoProducao.aspx/GetUsuarioCartaoAcesso",
          data: JSON.stringify({ codCartaoAcesso: p1 }),
          contentType: "application/json; charset=utf-8",
          dataType: "json",
          success: function (result) {
              if (result.length == 4) {
                  $("#divAlerta").css("display", "");
                  return false;
              } else {
                  $("#divAlerta").css("display", "none");
                  var json = JSON.parse(result);
                  //$("#txtQuantidade").val(json.IdUsuario);
                  opener.document.getElementById("hiddenIdUsuario").value = json.IdUsuario;
                  opener.document.getElementById("txtUsuarioProducao").value = json.Funcionario.ContaUsuarioDominio;
                  window.opener.document.form1.submit();
                  window.parent.close();
              }
          },
          failure: function (response) {
              alert(response.d);
          }
      });
  }

This code has been working since 2018. This week it started having problems. When this code is executed, I receive the following error message


Uncaught SyntaxError: "[object Object]" is not valid JSON
    at JSON.parse (<anonymous>)
    at Object.success (cadRecebimentoProducao.aspx?IdSolicitacaoAnalise=359139&Quantidade=2&Unidade=LI:153:41)
    at j (jquery.min.js:2:26925)
    at Object.fireWith [as resolveWith] (jquery.min.js:2:27738)
    at x (jquery.min.js:4:11253)
    at XMLHttpRequest.<anonymous> (jquery.min.js:4:14765)

I inserted console.log(result) before json.parse, and put the json that appeared in a validator, and the json was ok. Does anyone know what can it be?

0

1 Answer 1

1

Your result is already parsed, just use it, otherwise parsing it throws an error.

A simplest fix would be (remove the JSON.parse line):

opener.document.getElementById("hiddenIdUsuario").value = result.IdUsuario;
opener.document.getElementById("txtUsuarioProducao").value = result.Funcionario.ContaUsuarioDominio;

You pass dataType:'json' to $.ajax thus asking jQuery to parse the returned result as JSON for you.

I guess you could install a new jQuery version and it stopped working. Or even the server has started returning different HTTP headers. jQuery decides whether to parse JSON based on several factors like MIME type returned from the server, dataType options and maybe more.

You could read the docs: https://api.jquery.com/jQuery.ajax/ (scroll down to the dataType option).

JSON.parse({})

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.