Вот мое решение CORS, если оно вам пригодится.Добавьте класс глобального приложения в мое приложение-службу wcf.
Global.asax
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With,Accept");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
}
}
Файл web.config на сервере
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="mybinding">
<security mode="Transport">
<transport clientCredentialType="Basic" proxyCredentialType="Basic"></transport>
</security>
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior>
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true">
</serviceHostingEnvironment>
<protocolMapping>
<add scheme="https" binding="webHttpBinding" bindingConfiguration="mybinding"/>
<add scheme="http" binding="webHttpBinding" />
</protocolMapping>
</system.serviceModel>
Затем установите сертификат службы сервера на клиенте (где мы запускаем JS).
JS.
<script>
function make_base_auth(user, password) {
var tok = user + ':' + password;
var hash = window.btoa(tok); //javascript built-in function
return "Basic " + hash;
}
$(function () {
$.ajax({
type: "GET",
url: "https://vabqia130vm:12001/service1.svc/getdata?value=34",
contentType: "application/json;charset=utf-8",
beforeSend: function (req) {
req.setRequestHeader("Authorization", make_base_auth("administrator",
"abcde12345!"));
},
success: function (d) {
console.log(d);
},
});
})
</script>
Результат.
Не стесняйтесь, дайте мне знать, если есть что-то, с чем я могу помочь.