简体   繁体   English

从jQuery和JSON调用WCF REST服务时的Access-Control-Allow-Origin

[英]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. 任何帮助,将不胜感激。

这意味着您请求的WCF服务与调用它的应用程序的来源不同,因此受同源策略”的限制。您必须使用JSONP(服务必须支持jsonp)或CORS(服务器端应该添加允许您的源的CORS头)

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.

相关问题 “Access-Control-Allow-Origin:*”对REST Web服务没有影响 - “Access-Control-Allow-Origin:*” has no influence in REST Web Service jQuery XML REST Access-Control-Allow-Origin - jQuery XML REST Access-Control-Allow-Origin asmx web服务 - jQuery ajax post json(500错误) - CORS(Access-Control-Allow-Origin设置为*) - asmx web service - jQuery ajax post json (500 error) - CORS (Access-Control-Allow-Origin is set to *) jQuery Json错误,Steam中的json,访问控制-允许-来源 - Jquery Json error, json from steam, Access-Control-Allow-Origin 当我无权访问服务器时,如何让jQuery调用REST服务而不出现Access-Control-Allow-Origin错误? - How do I get jQuery to call REST service without getting Access-Control-Allow-Origin error when I have no access to the server? jQuery .load()访问控制允许来源 - jQuery .load() Access-Control-Allow-Origin jQuery使用WCF。 错误:XMLHttpRequest无法加载Access-Control-Allow-Origin不允许使用Origin null - Jquery to consume WCF. error: XMLHttpRequest cannot load Origin null is not allowed by Access-Control-Allow-Origin “Access-Control-Allow-Origin不允许使用Origin null。”使用jQuery调用Web服务时出错 - “Origin null is not allowed by Access-Control-Allow-Origin.” Error when calling a web service with jQuery jQuery解析Json“ Access-Control-Allow-Origin不允许原始null” - Jquery to parse Json “Origin null is not allowed by Access-Control-Allow-Origin” Jquery (jfeed) - Access-Control-Allow-Origin 不允许来源 xxxxx - Jquery (jfeed) - Origin xxxxx is not allowed by Access-Control-Allow-Origin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM