1
I created a Web API that returns a JSON
and who is having a problem similar to the question JSONP: status code 200 OK and still returns $.Ajax(... error:Function()...).
Client consumes the API with Nodes like this:
refresh() {
axios
.get(`${URL}"${this.generateDate()}"`)
.then(resp => {
console.log(resp);
this.setState({ ...this.state, services: resp.data, loading: false });
let statusModal = swal.getState();
//if (!statusModal.isOpen) {
// swal.close();
//}
})
.catch(e => {
console.log('O erro é =>', e);
//swal('Ocorreu algum problema!', 'Entre em contato com o responsável', 'error');
//this.setState({ ...this.state, loading: false });
});
}
The return of JSON is 200, but in the console displays the error described below
I memorized the API method with the Cors to obtain the conpatibility of origins, however the error persists.
My question is:
Just this snippet of code where I memorize the api method is enough ?
This is the API method: improved at the suggestion of Danilo Ribeiro da Silva
[EnableCors(origins: "*", headers: "*", methods: "*", exposedHeaders: "*")]
[RoutePrefix("api/monitora")]
public class MonitoraController : ApiController
{
// GET: api/Painel/5
[Route("GetPainel")]
public IHttpActionResult GetPainel(string dtHoraExecuta)
{
try
{
DateTime dt = DateTime.MinValue;
if (!DateTime.TryParse(dtHoraExecuta.Replace("\"", ""), out dt))
return StatusCode(HttpStatusCode.NotAcceptable); // 406
List<ProductType> lreturn = new List<ProductType>();
PainelMonitoracaoBusiness obj = new PainelMonitoracaoBusiness();
dt = Convert.ToDateTime(Convert.ToDateTime(dtHoraExecuta.Replace("\"", "")));
var _painel = obj.ObterDadosPainel(dt);
if (_painel == null)
{
var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
{
Content = new StringContent(string.Format("Não foram encontrado dados para esse período. = {0}", dtHoraExecuta)),
ReasonPhrase = "Serviços não encontrados."
};
throw new HttpResponseException(resp);
}
for (int i = 0; i < _painel.Count; i++)
{
ProductType prd = new ProductType();
for (int z = 0; z < _painel[i].lservices.Count; z++)
{
if (_painel[i].ID_SERVICO == _painel[i].lservices[z].ID_SERVICO)
{
prd.productType = _painel[i].productType;
SubServico subServico = new SubServico();
subServico.statusAll = _painel[i].lservices[z].statusAll;
subServico.subtype = _painel[i].lservices[z].subtype;
Info infoAbertura = new Info();
infoAbertura.mount = _painel[z].lservices[0].info[0].mount;
infoAbertura.status = _painel[z].lservices[0].info[0].status;
infoAbertura.type = _painel[z].lservices[0].info[0].type;
Info infoFechamento = new Info();
infoFechamento.mount = _painel[z].lservices[0].info[1].mount;
infoFechamento.status = _painel[z].lservices[0].info[1].status;
infoFechamento.type = _painel[z].lservices[0].info[1].type;
subServico.info.Add(infoAbertura);
subServico.info.Add(infoFechamento);
prd.services.Add(subServico);
}
}
lreturn.Add(prd);
}
return Ok(lreturn);
}
catch (Exception)
{
return StatusCode(HttpStatusCode.ExpectationFailed); // 417
}
}
This is the error I captured in the browser:
erro é => TypeError: Cannot read property 'value' of undefined
Error image:
See which line of error
– Leandro Angelo