Below is part of my web api project.
public class TriviaController : ApiController
{
public HttpResponseMessage Get()
{
QuestionAnswer _QuestionAnswer = new QuestionAnswer();
TriviaQuestion _TriviaQuestion = _QuestionAnswer.GetQuestion("test@yahoo.com");
return Request.CreateResponse(HttpStatusCode.OK, _TriviaQuestion);
}
}
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
EnableCrossSiteRequests(config);
AddRoutes(config);
}
private static void AddRoutes(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "Default",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
private static void EnableCrossSiteRequests(HttpConfiguration config)
{
var cors = new EnableCorsAttribute(
origins: "*",
headers: "*",
methods: "*");
config.EnableCors(cors);
}
}
My client application receive json data which returned by contoller. Everything working correct until I modify to specific origins like below.
var cors = new EnableCorsAttribute(
origins: "https://jsfiddle.net/", //"https://localhost:44304/",
headers: "*",
methods: "*");
After that, my client application receive only null value and error message saying Status Code 0.
According to this reference link, I know that it is because of CORS.
Below is my client code. It is so simple.
alert("*");
$.getJSON("https://localhost:44300/api/trivia", function(data) {
alert(JSON.stringify(data));
})
.done(function() { alert('getJSON request succeeded!'); })
.fail(function(jqXHR, textStatus, errorThrown) {
alert('getJSON request failed! ' + textStatus + ' :: ' + jqXHR + ' :: ' + errorThrown);
$.each(jqXHR, function(k, v) {
//display the key and value pair
alert(k + ' is ' + v);
});
})
.always(function() { alert('getJSON request ended!'); });
To see on jsFiddle, click here.
So my question is,
Is it possible to set allow origin using SSL? If so what am I doning wrong?