I have a Cordova 7.0.1 application that makes a request to a method from an Asp.Net MVC Web API 4.6 site. When the call is made on the device, cordova run andoid , the method execution returns error.
IfyourunthebrowserdirectcallthroughanHTMLpageorruntheAppwithruncordovabrowser,itworks.
ThecontrollermethodCategoryApiController.csisthis:
publicclassCategoriaApiController:ApiController{privateDBContextdb=newDBContext();[HttpGet]publicstringListaCategorias(Categoriacategoria){varlistaCategorias=(dynamic)null;try{listaCategorias=db.Categoria.OrderBy(x=>x.Descricao).ToList();returnJsonConvert.SerializeObject(listaCategorias);//return"user-error";
}
catch (Exception ex)
{
return JsonConvert.SerializeObject(listaCategorias);
//return "user-error";
}
}
}
The WebApiConfig.cs class:
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.
Are there any settings in the App or Site to allow this request to work?