<access origin="*" />
<system.webServer> <httpProtocol> <customHeaders> <add name="Access-Control-Allow-Methods" value="OPTIONS,POST,GET"/> <add name="Access-Control-Allow-Headers" value="x-requested-with,content-type"/> <add name="Access-Control-Allow-Origin" value="*" /> </customHeaders> </httpProtocol> </system.webServer>
$.ajax({ url: "http://localhost/CROS/handler.ashx", type: "POST", dataType: "json", success: function (data) { console.log(data); } });
Ext.Ajax.request({ url: 'http://localhost/CROS/Handler.ashx', method: 'POST', success:function(resp){ Ext.Msg.alert('success', resp.responseText); }, failure:function(resp){ Ext.Msg.alert('failure', resp.responseText); } });
四、在Response中设置Header
这是针对单个请求(以Asp.Net为例)
public class Handler : IHttpHandler { public void ProcessRequest(HttpContext context) { #region 支持跨域请求 context.Response.ClearHeaders(); context.Response.AppendHeader("Access-Control-Allow-Origin", "*"); context.Response.AppendHeader("Access-Control-Allow-Headers", "X-Requested-With, Content-Type"); context.Response.AppendHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS"); #endregion context.Response.ContentType = "application/json"; context.Response.Write("{a: 1, b: 2}"); } }