zoukankan      html  css  js  c++  java
  • 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); }     } }

  • 相关阅读:
    适用于SQL Server生产环境DBA的七大技巧
    Android网络收音机项目(源码实例分享)
    利用antzip包来进行解压与压缩
    开发网站客户端第二弹
    Android 4.1源代码今日将发布
    Android 4.1 Jelly Bean(果冻豆) SDK4.1最新下载
    Google I/O 2012 主题演讲直播(第一天)Android 4.1 Jelly Bean们来了
    android水果连连看开发实例【源码下载有背景音乐、音效】
    android魔法泡泡动画分析(附源码)
    优亿移动开放日第十五期:优亿开发行业数据报告
  • 原文地址:https://www.cnblogs.com/yipeng-yu/p/7462941.html
Copyright © 2011-2022 走看看