Error sending Json

1

I have the following variable:

json_envio = JSON.stringify (obj);

The value of it is:

 "{"usuario":
     {"login":"gleyson",
      "senha":"1"},
 "razao_social":"INTELIDER",
 "nome_fantasia":"INTELIDER LTDA",
 "cpf_cnpj":"10999558000186",
 "rg_insc_estadual":"11111",
 "tipo":"F"}"

I am using Jquery Ajax to send, however, this is falling into the code exception below :

$.ajax({
      type: "POST",
      url: "http://localhost/api/pessoas",
      dataType: "json",
      async: false,
      data: json_envio,
      success: function (result) {
          alert('tudo certo');
      },
      error: function (exception) { alert('Exeption:' + JSON.stringify(exception)); }

The error is as follows:

Exeption:{"readyState":4,"responseText":"{\"Message\":\"
A solicitação é inválida.\",\"ModelState\":{\"pessoa.tipo\":[\"O campo tipo 
é obrigatório.\"],\"pessoa.razao_social\":[\"O campo razao_social é 
obrigatório.\"]}}","responseJSON":{"Message":"A solicitação é 
inválida.","ModelState":{"pessoa.tipo":["O campo tipo é 
obrigatório."],"pessoa.razao_social":["O campo razao_social é 
obrigatório."]}},"status":400,"statusText":"Bad Request"}

By mistake I understand that it is complaining about the field type and reasonsocial, however, both are in Json which is in the variable json_envio and taking the value of it and using the soapUI, I can send without problem. What could be wrong?

image of the value of the obj:

    
asked by anonymous 27.06.2017 / 16:00

1 answer

3

Two things can happen here:

  • Content type missing

Add the contentType property with the "application/json" value in the ajax function object. This causes the server to understand that you are sending a Json and not an HTML form. I.e.:

$.ajax({
  type: "POST",
  contentType: "application/json", // <------
  url: "http://localhost/api/pessoas",
  dataType: "json",
  async: false,
  data: json_envio,
  success: function (result) {
      alert('tudo certo');
  },
  error: function (exception) { alert('Exeption:' + JSON.stringify(exception)); }
  • Application of JSON.stringify to valid JSON

Your object was already formatted as a valid JSON. When you apply JSON.stringify again, you "break" it for your model - it remains a valid JSON, but now the property names include double quotation marks. The application that receives the model via ajax can not recognize these properties as the properties it was expecting. If it's something like ASP.NET, you'll see the properties as null or with their default values in the debugger.

Try these two codes in the browser console and see the difference:

var x = {foo: 10};
JSON.stringify(x);

And compare with the result of:

var x = {"foo": "10"};
JSON.stringify(x);

To not have to worry if your objects are already valid JSON or not, ideally you keep a unique pattern in your code. Either declares everything as JSON and does not use JSON.stringify , or declares everything as a non-JSON JavaScript object and always uses JSON.stringify before using it for whatever.

    
27.06.2017 / 16:38