jquery call cross-domain webapi owin self-host

<!DOCTYPE HTML>
<html LANG="cn">
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>cross domain IE5-11</title>
    <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
    <script>
         $(function(){
			$.support.cors = true; // support to IE5-11
			$.ajax({
				type: "GET",
				url: "http://xxx-services:8081/api/values/99",
				dataType: "json"
			}).done(function (data) {
				console.log(data);
			});
        });
    </script>
</head>
<body>
</body>
</html>

public class ValuesController : ApiController
{
        public async Task<string> Get(int id)
        {
            var info = string.Format("API CurrentThread:{0}", Thread.CurrentThread.ManagedThreadId);
            var infoTask = await GetCurrentThread();
            var infoTaskFinished = string.Format("After GetCurrentThread:{0}", Thread.CurrentThread.ManagedThreadId);
            return string.Format("{0},{1},{2},{3}", info, infoTask, infoTaskFinished, id);
        }

        private async Task<string> GetCurrentThread()
        {
            await Task.Delay(1500);
            return string.Format("Action CurrentThread:{0}", Thread.CurrentThread.ManagedThreadId);
        }
}
public class Startup
{
    public void Configuration(IAppBuilder MyApp)
    {
            HttpConfiguration config = new HttpConfiguration();
            config.Routes.MapHttpRoute(name: "DefaultApi", routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
                );
       MyApp.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); MyApp.UseWebApi(config); } }
//window service hosting

public
class MyControllersService : ServiceHelper.BaseService { bool isstart = false; public MyControllersService() : base("MyControllersService", true) { } protected override void MyWork() { try { this.OutputLog("MyWork Start", this.ServiceName); string address = "http://xxx-services:8081/"; if(!isstart) { WebApp.Start<Startup>(url: address); isstart = true; this.OutputLog("Starting OWIN Host", this.ServiceName); } } catch (Exception err) { this.OutputLog(CommonHelper.CommonUnit.ExceptionMsg(err), this.ServiceName); throw; } finally { this.OutputLog("MyWork Stop", this.ServiceName); }     } }

 

posted @ 2017-09-01 14:04  Yu  阅读(253)  评论(0编辑  收藏  举报