Errror 500 using Ajax and POST

I’m trying to connect my app to a REST web service. Below is my code. I receive a 500 error. I know that 500 error is a server error but if I use RestClient app, I receive a 201 response code and the result as I expect to.

Data sent using RestClient is exactly the same as “dato” variable.

Do you have an idea to solve it?
Thanks.
David.

My code:

Button1.onclick=function(){
URL=“http://dibse.ddns.net/NCWebAPI/api/CuentasAbiertas”;
encURL=encodeURI(URL);

  dato='{"Cajero": 1,"Detalles": [{ "CodigoArticulo": "01000","Cantidad": "1.0"}]}';

  Label1.value=dato;
  
  req=Ajax(encURL,"POST",dato);
  
  if (req.status==200 || req.status==201) {
          Label2.value=req.responseText;
  } else {
          Label2.value="Error: "+req.status;
  }

}

Try setting your content type to JSON and stringify your dato variable.

Thank you for your help.

It finally works in this way:

  URL="http://dibse.ddns.net/NCWebAPI/api/CuentasAbiertas";
  encURL=encodeURI(URL);

  var obj = { Cajero: 1,Detalles: [{ CodigoArticulo: "01000",Cantidad: 1.0 }] };
  var dato = JSON.stringify(obj);
  Label1.value=dato;

  req=Ajax(encURL,"POST",obj);
  
  if (req.status==200 || req.status==201) {
          Label2.value=req.responseText;
  } else {
          Label2.value="Error: "+req.status + " >> " + req.responseText;
  }
1 Like