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); }     } }

  • 相关阅读:
    一文带你看清HTTP所有概念
    程序员不得不了解的硬核知识大全
    看完这篇HTTP,跟面试官扯皮就没问题了
    ReentrantLock 源码分析从入门到入土
    计算机网络的核心概念
    Kafka 的这些原理你知道吗
    2019 我是怎么熬过来的?
    不懂什么是锁?看看这篇你就明白了
    机器学习——方差、协方差与皮尔逊值
    最小生成树的本质是什么?Prim算法道破天机
  • 原文地址:https://www.cnblogs.com/yipeng-yu/p/7462941.html
Copyright © 2011-2022 走看看