Though I accepted E.B. answer, I just liked to re-write the full working code below, as it could be helpfull for some one in the future:
project.json
:
{
"version": "1.0.0-*",
"buildOptions": {
"debugType": "portable",
"emitEntryPoint": true
},
"dependencies": {
"Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
"Microsoft.AspNetCore.Mvc": "1.0.1",
"Microsoft.AspNetCore.StaticFiles": "1.0.0",
"Microsoft.AspNetCore.Cors":"1.0.0"
},
"frameworks": {
"netcoreapp1.0": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.1"
}
},
"imports": "dnxcore50"
}
}
}
Program.cs
:
using System.IO; // for Directory
using Microsoft.AspNetCore.Hosting; // for WebHostBuilder()
namespace ApiCall
{
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.Build();
host.Run();
}
}
}
Startup.cs
:
using Microsoft.AspNetCore.Builder; // for IApplicationBuilder and FileServerOptions
using Microsoft.AspNetCore.Hosting; // for IHostingEnvironment
using Microsoft.Extensions.DependencyInjection; // for IServiceCollection
namespace ApiCall
{
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(); // to be accesssed from external sites, i.e. outside the server
// Add framework services.
services.AddMvc(); // to use Controllers
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseFileServer(); // to be accessed from files in the server inside wwwroot folde
app.UseCors(builder => // to be accesssed from external sites, i.e. outside the server
builder.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials()
);
app.UseMvc();
}
}
}
Controllers / FetchController.cs
:
using System; // for Console.WriteLine
using Microsoft.AspNetCore.Mvc; // for Controller, [Route], [HttpPost], [FromBody], JsonResult and Json
namespace ApiCall.Controllers
{
[Route("api/[controller]")]
public class FetchController : Controller
{
// POST api/values
[HttpPost]
public JsonResult Post([FromBody]object loginParam)
{
Console.WriteLine(loginParam);
return Json(loginParam);
}
}
}
wwwroot / index.html
:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<h2>
Test page for using ajax call in webapi core.
</h2>
<button id="btnUseFetch" onclick="useFetch()" class="btn btn-danger">Use Fetch</button>
<script type="text/javascript">
function useFetch() {
var loginParam = JSON.stringify({ email: 'hasan@gmail.com', pswd: '1234' });
fetch('/api/Fetch', { // or 'http://localhost:5000/api/Fetch' in external file
method: 'post',
headers: new Headers({
'Content-Type': 'application/json'
}),
body: loginParam
}).then(function(response) {
return response.json();
}).then(function(returnedValue) {
var asdf = returnedValue;
console.log(asdf);
}).catch(function (err) {
console.log(JSON.stringify(err));
});
}
</script>
</body>
</html>