Cannot catch error using jQuery... Any clue?
JS
var url = "http://localhost:57371/api/Apps/";
$.ajax({
url: url,
type: "get",
success: function (response) {
alert("second success");
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.responseText);
}
});
ASP.NET WEBAPI 2
[Authorize]
public class AppsController : ApiController
{
[Route("api/Apps/")]
public IEnumerable<App> Get()
{
var r = new List<App>();
using (var ctx = new REDEntities())
{
if (ctx.Apps.Any())
r.AddRange(ctx.Apps.ToList());
}
return r.ToList();
}
}
Console
jquery-3.1.1.js:9536 GET http://localhost:57371/api/Apps/ 401 (Unauthorized) send @ jquery-3.1.1.js:9536 ajax @ jquery-3.1.1.js:9143 (anonymous) @ Index:96 dispatch @ jquery-3.1.1.js:5201 elemData.handle @ jquery-3.1.1.js:5009 Index:1 XMLHttpRequest cannot load http://localhost:57371/api/Apps/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:57166' is therefore not allowed access. The response had HTTP status code 401.