Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从jQuery和JSON调用WCF REST服务时的访问控制允许源_Jquery_Json_Wcf_Rest - Fatal编程技术网

从jQuery和JSON调用WCF REST服务时的访问控制允许源

从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({

我有以下代码

WCF休息方法

    [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>
 ...

...

通过将其设置为*,我们允许来自任何来源的请求访问该服务