I have been working on a web API REST service in asp.net 5 and encountered an issue. The website is able to successfully send a request to the service, but when the service sends back the request options to the website, the response received says HTTP 1/1 404 Not Found
.
Here is a Fiddler Screenshot of the "Options" request
The controllers for the service are configured by:
public class Startup {
public void ConfigureServices (IServiceCollection services) {
services.AddCors();
services.AddMvc();
}
}
One of the functions that the website needs to access is:
[Route("/operator")] // localhost:5000/operator
public class OperatorController : Controller
[EnableCors(origins:'*', headers>'*', methods:'*');
public IActionResult Index()
{
return new HttpOkObject("Hello, select your operation");
}
The website accesses this function (by sending the route to the class which calls Index) using ajax
<script type="text/javascript">
$(document).ready(function() {
var options = {};
options.url = "http://localhost:5000/installments";
options.type = "GET";
options.contentType = "application/jsonp";
// options.dataType="jsonp";
options.success = function (results) {
alert(results.join);
};
options.error = function(evt){
alert(evt.status + " - " + evt.statusText)
};
$.ajax(options);
});
</script>
[EDIT] To address the error, I added the following line:
<pre><code>app.Use(async (context, next) => { context.Response.Headers.Append("Access-Control-Allow-Origin", "*"); await next(); });</code></pre>
This line was added before
app.UseMvc();
While this adds the required header, it unfortunately resulted in the error message
“Response for preflight has invalid HTTP status code 404”