Error while sending Json

Asked

Viewed 256 times

1

I have the following variable:

json_sending = JSON.stringify(obj);

the value of the:

 "{"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 falling in the exception of the code 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"}

I understood that he is complaining about the type and social razao_field, however, both are in Json that is in the json_send variable and taking the value of it and using soapUI, I can send without problem. What could be wrong?

image of obj value:

inserir a descrição da imagem aqui

  • 1

    Why convert json object to string if dataType is json? I don’t seem to need JSON.stringify(obj);

  • 1

    @Lucascosta O dataType defines the type of data expected in the server response, not from the data sent to the server.

  • The question is: what code on /api/pessoas?

  • @Andersoncarloswoss is a Webapi service, but if I can send by souap should I not be able to send by ajax as well? You want me to enter the entity code and the controller?

  • 1

    Yes. You came to check which data comes in the API this way?

  • 2

    @Intelidersistemas can send only obj in the data:. jQuery already converts it, so you don’t need JSON.stringify(obj)

  • @Sergio added the image I picked up in the debug of Chrome in my question

  • good idea @Andersoncarloswoss, I’ll look here.

  • 1

    @Sergio was just that, I sent directly the object without the JSON, Stringify and passed blz. thanks for the attention!

Show 4 more comments

1 answer

3


Two things can happen here:

  • Missing type of content

Add the property contentType with the value "application/json" in the ajax function object. This makes the server 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 the valid JSON

Your object was already formatted as a valid JSON. When you apply JSON.stringify Again, you "break" your template - it remains a valid JSON, but now the property names include double quotes. The application that receives the model via ajax cannot recognize these properties as the properties it was expecting. If it is something like ASP.NET, you will 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);

In order not to have to worry whether your objects are already valid JSON or not, the ideal is for you to maintain a unique pattern in your code. Or declares everything as JSON and does not use the JSON.stringify, or declares everything as non-json Javascript object and always uses the JSON.stringify before using for whatever.

  • I changed the date: json_upload 'to 'date: obj' as you suggested and it worked blz, thanks for your attention!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.