简体   繁体   English

使用BasicHttpbinding对WCF服务进行跨域JQuery Ajax调用

[英]Cross-Domain JQuery Ajax call to WCF Service with BasicHttpbinding

I have a WCF service with "BasicHttpbinding" binding in place.I can gracefully call the service using JQuery in Internet Explorer. 我有一个带有“ BasicHttpbinding”绑定的WCF服务。我可以在Internet Explorer中使用JQuery优雅地调用该服务。 But the same code does not work in Chrome or FireFox. 但是,相同的代码在Chrome或FireFox中不起作用。 I can understand there is a Cross-Domain issue with JQuery Ajax call from Chrome or FireFox. 我可以理解,Chrome或FireFox的JQuery Ajax调用存在跨域问题。 I know I can expose a "webHttpBinding" binding with crossDomainScriptAccessEnabled="true" and call the service. 我知道我可以使用crossDomainScriptAccessEnabled =“ true”公开“ webHttpBinding”绑定并调用服务。 But if there is a scanario that , I have to go with the existing "BasicHttp" binding then what is the solution? 但是,如果存在scanario,则必须使用现有的“ BasicHttp”绑定,那么解决方案是什么? I have called the WCF service with the following JQuery code: 我已经使用以下JQuery代码调用了WCF服务:

      $.ajax({
                  type: "POST",
                  url: "http://localhost:1415/MyService.svc/bh/",
                  data: bhRequest,
                  timeout: 10000,
                  contentType: "text/xml",
                  dataType: "xml",
                  beforeSend: function (xhr) {
                      xhr.setRequestHeader("SOAPAction", "http://tempuri.org/IMyService/GetData");
                  },
                  success: function (data) {
                      $(data).find("GetDataResponse").each(function () {
                          alert($(this).find("GetDataResult").text());
                      });
                  },
                  error: function (xhr, status, error) {
                      alert(error);

                  }
              });

My WCF service method is as following: 我的WCF服务方法如下:

 [OperationContract]
 string GetData(int value);

Could anybody provide any solution on this? 有人可以提供任何解决方案吗?

Add global.asax to your WCF and afterwards add this: 将global.asax添加到您的WCF中,然后添加以下内容:

protected void Application_BeginRequest(object sender, EventArgs e)
{
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin","*");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM