[英]Access-Control-Allow-Origin when calling WCF REST Service from jQuery and JSON
I have the following code. 我有以下代码。
WCF REST method WCF REST方法
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "Account")]
[OperationContract]
string GetUniqueLoginId();
Client Side Call 客户端呼叫
$.ajax({
type: "GET",
url: serviceURL + 'Account',
contentType: 'application/json; charset=utf-8',
dataType: "json",
success: app.onGetUniqueId,
error: app.onAjaxError
});
When I use IE (11), it works perfectly by returning a unique id. 当我使用IE(11)时,它通过返回唯一ID完美地工作。 But when I use chrome, it gives me the following error. 但是当我使用chrome时,它会给我以下错误。
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:5553' is therefore not allowed access.
How to resolve the issue? 如何解决这个问题? Any help would be appreciated. 任何帮助,将不胜感激。
You need to enable CORS on a service hosted on IIS or IIS Express by adding the following configuration in the web config file. 您需要在Web配置文件中添加以下配置,在IIS或IIS Express上托管的服务上启用CORS。
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
...
By setting it to *, we are allowing requests from any origin to access the service. 通过将其设置为*,我们允许来自任何来源的请求访问该服务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.