从jQuery和JSON调用WCF REST服务时的访问控制允许源
我有以下代码 WCF休息方法从jQuery和JSON调用WCF REST服务时的访问控制允许源,jquery,json,wcf,rest,Jquery,Json,Wcf,Rest,我有以下代码 WCF休息方法 [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "Account")] [OperationContract] string GetUniqueLoginId(); 客户端呼叫 $.ajax({
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "Account")]
[OperationContract]
string GetUniqueLoginId();
客户端呼叫
$.ajax({
type: "GET",
url: serviceURL + 'Account',
contentType: 'application/json; charset=utf-8',
dataType: "json",
success: app.onGetUniqueId,
error: app.onAjaxError
});
当我使用IE(11)时,它通过返回一个唯一的id完美地工作。但是当我使用chrome时,它会给我以下错误
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:5553' is therefore not allowed access.
如何解决这个问题?任何帮助都将不胜感激。这意味着您请求的WCF服务与调用它的应用程序位于不同的来源,因此受到限制。您必须使用JSONP(服务必须支持JSONP)或CORS(服务器端应添加允许您的来源的CORS头)您需要通过在web配置文件中添加以下配置,在IIS或IIS Express上承载的服务上启用CORS
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
...
...
通过将其设置为*,我们允许来自任何来源的请求访问该服务