错误WCF - 请求的资源上没有“Access-Control-Allow-Origin”标头

时间:2014-05-19 13:14:35

标签: jquery html ajax wcf

HTML Jquery Ajax HTML页面正在http://domain2.application.net/index.html中运行。

$.ajax({
                type: "POST",
                url: "http://domain.service.net/Service.svc/AddUsers",
                contentType: 'application/json; charset=utf-8',
                data: dataToSend,
                dataType: 'json',
        async: false,

                success: function (response) {        
                }
            });

WCF服务

[OperationContract]
[WebInvoke(UriTemplate = "", Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public void AddUsers(string name, string email)
{            
    //Add user information to database          
}

调用AddUser服务时出现以下错误 的错误

> XMLHttpRequest cannot load
> http://domain.service.net/Service.svc/AddUsers. No
> 'Access-Control-Allow-Origin' header is present on the requested
> resource. Origin 'http://domain2.application.net' is therefore not
> allowed access.

浏览器标头值

Request URL:http://domain.service.net/Service.svc/AddUsers
Request Method:OPTIONS
Status Code:405 Method Not Allowed
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,ta;q=0.6
Access-Control-Request-Headers:access-control-allow-origin, accept, access-control-allow-credentials, x-requested-with, access-control-allow-headers, access-control-allow-methods, content-type
Access-Control-Request-Method:POST
Connection:keep-alive
Host:domain.service.net
Origin:http:http://domain2.application.net
Referer:http:http://domain2.application.net/index.html
User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/537.36

1 个答案:

答案 0 :(得分:-1)

如果您查看here,您会发现问题与后端有关,因此为了解决这个问题,您必须向WCF项目添加几个帮助程序,并且还要显式修改web.config

的Web服务端点
<crossOriginResourceSharingBehavior />

可能的解决方案是here at this site

希望它有所帮助!祝世界CORS好运!