httpclient后台和ajax请求跨域
1、httpclient
$("#btnGet").on("click", function () { $.post("WebService.asmx/GetWebService", function (d) { $("#getContent").html(d); }); });
<input type="button" id="btnGet" value="按钮client"/> <div id="getContent"></div>
using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Configuration; using System.IO; using System.Linq; using System.Net.Http; using System.Text; using System.Web; using System.Web.Services; /// <summary> /// WebService 的摘要说明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 // [System.Web.Script.Services.ScriptService] public class WebService : System.Web.Services.WebService { public static readonly string url = ConfigurationManager.AppSettings["url_get"].ToString(); public WebService () { //如果使用设计的组件,请取消注释以下行 //InitializeComponent(); } [WebMethod] public string HelloWorld() { return "Hello World"; } [WebMethod] public void GetWebService() { string result = ""; using (HttpClient client = new HttpClient()) { client.BaseAddress = new Uri(url); try { using (Stream stream = client.GetStreamAsync(url).Result) { using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { result = reader.ReadToEnd(); } } } catch (Exception ex) { throw ex; } } Context.Response.Write(result); Context.Response.End(); } }
<appSettings>
<add key="url_get" value="http://10.100.22.54:8095/api/BookChapters"/>
</appSettings>
webpai
using System.Collections; using System.Collections.Generic; using System.Web.Http; using WebApplication1.Models; using System.Linq; using Newtonsoft.Json; using WebApplication1.App_Start; namespace WebApplication1.Controllers { [AllowCrossSiteJson] public class BookChaptersController:ApiController { // GET api/bookchapters [HttpGet] public IEnumerable<BookChapter> Get() { return chapters; } // GET api/bookchapters/5 public BookChapter Get(int id) { return chapters.Where(c => c.Number == id).SingleOrDefault(); } // POST api/bookchapters [AcceptVerbs("POST")] [ActionName("bookchapter")] public void PostBookChapter([FromBody]BookChapter value) { chapters.Add(value); } // PUT api/bookchapters/5 public void PutBookChapter(int id, [FromBody]BookChapter value) { chapters.Remove(chapters.Where(c => c.Number == id).Single()); chapters.Add(value); } // DELETE api/bookchapters/5 public void DeleteBookChapter(int id) { chapters.Remove(chapters.Where(c => c.Number == id).Single()); } private static List<BookChapter> chapters; static BookChaptersController() { chapters = new List<BookChapter>() { new BookChapter{Number=1,Title=".Net Architecture",Pages=20}, new BookChapter{Number=2,Title=".Net ",Pages=120}, new BookChapter{Number=3,Title=".Net a",Pages=480}, new BookChapter{Number=4,Title=".Net b",Pages=200}, new BookChapter{Number=5,Title=".Net c",Pages=210}, new BookChapter{Number=17,Title=".Net d",Pages=230}, new BookChapter{Number=42,Title=".Net e",Pages=270}, new BookChapter{Number=43,Title=".Net f",Pages=320}, new BookChapter{Number=50,Title=".Net g",Pages=520} }; } } }
2、ajax
$("#btnGetClient").on("click", function () { $.get("http://10.100.22.54:8095/api/BookChapters", function (d) { $("#getContent").html(JSON.stringify(d)); }); })
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Http.Filters; namespace WebApplication1.App_Start { public class AllowCrossSiteJsonAttribute : ActionFilterAttribute { public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext) { if (actionExecutedContext.Response != null) { actionExecutedContext.Response.Headers.Add("Access-Control-Allow-Origin", "*"); } base.OnActionExecuted(actionExecutedContext); } } }

浙公网安备 33010602011771号