0
I have a Cordova 7.0.1 application that requests a method from an Asp.Net MVC Web API 4.6 website. When the call is made on the device, Cordova run Andoid, execution of the method returns error.
If I run the call directly from the browser, through an HTML page or run the App with run Cordova browser, works.
The controller method Categoriaapicontroller.Cs is this:
public class CategoriaApiController : ApiController
{
private DBContext db = new DBContext();
[HttpGet]
public string ListaCategorias(Categoria categoria)
{
var listaCategorias = (dynamic)null;
try
{
listaCategorias = db.Categoria.OrderBy(x => x.Descricao).ToList();
return JsonConvert.SerializeObject(listaCategorias);
//return "user-error";
}
catch (Exception ex)
{
return JsonConvert.SerializeObject(listaCategorias);
//return "user-error";
}
}
}
The class Webapiconfig.Cs:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
EnableCrossSiteRequests(config);
config.Routes.MapHttpRoute(
name: "AppLogin",
routeTemplate: "api/login/{controller}/{action}/{id}"
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
private static void EnableCrossSiteRequests(HttpConfiguration config)
{
var cors = new EnableCorsAttribute(
origins: "*",
headers: "*",
methods: "*");
config.EnableCors(cors);
}
}
And called ajax:
function chamaListaCategorias(){
// var categoria = new CategoriaController();
// categoria.ListaCategorias();
var url = BASE_URL + "api/CategoriaApi/ListaCategorias";
$.ajax({
type: "GET",
dataType: "json",
success: function (data, status, jqXHR) {
$("#res").html("Local success callback.<br>") ;
$("#res").html(data);
},
error: function (jqXHR, status, err) {
$("#res").html("Local error callback." + err + ' --- ' + status + ' ---' + jqXHR);
console.log(err);
}
}
I installed the Whitelist plugin that in my understanding was to configure this type of access.
Have any settings on the App or Site to allow this request to work?