2

好像我有跨域访问问题。我已经看到一些解决方案表明要添加“Access-Control-Allow-Origin:*”,但我不知道在哪里可以做到这一点。

我需要创建一些处理程序吗?

我正在使用 WCF Web API。

错误:XMLHttpRequest 无法加载http://localhost:8081/Song/0。Access-Control-Allow-Origin 不允许来源http://localhost:8080 。

编辑

我注意到这只发生在 HTTP 方法为 PUT 或 DELETE 时。我可以使用 GET 或 POST 成功发出请求。

我正在使用 jquery 发出请求。

$.ajax({
        url: Settings.RESTfulEndPointFor('Song/' + songID),
        type: 'DELETE',
        success: function (response) {
            callback(response);
        }
    });

我不知道为什么,但这似乎导致方法 OPTIONS 带有 Access-Control-Request-Method: DELETE。

有谁知道这是什么原因造成的?

任何帮助表示赞赏。

4

6 回答 6

4

通过 AJAX 调用连接到 WCF RESTful 服务时遇到了这个问题

我的javascript是这样的:

var GetData= function(){

    var data;
    $.ajax({
        url:  this.server + "/data",
        async: false,
        type: "GET",
        success: function (success) {
            data = success;
        }
    });
    return data;

};

我的服务端点是用这段代码打开的

ServiceHost host = new ServiceHost(new MyService());
host.Open();

所有重要数据都存储在 App.config 文件中,我不必为此修复更改该文件。

我知道在发送响应消息之前我必须在某处添加标题。

经过一番搜索和破解后,我找到了 ServiceHost 对象的 Authorization 属性。Authorization 属性是 ServiceAuthorizationBehavior 类的一个实例,其对象具有一个名为 ServiceAuthorizationManager 的属性,该属性是 ServiceAuthorizationManager 类的一个实例。

通过创建一个继承自 ServiceAuthorizationManager 的新类并将其设置为您的 ServiceHost 实例的授权行为的 ServiceAuthorizationManager 属性,您可以拦截对您的服务的所有调用。

这就是我实施课程的方式

public class MyServiceAuthorizationManager : ServiceAuthorizationManager
{
protected override bool CheckAccessCore(OperationContext operationContext)
{

    HttpResponseMessageProperty prop = new HttpResponseMessageProperty();
    prop.Headers.Add("Access-Control-Allow-Origin", "*");
    operationContext.OutgoingMessageProperties.Add(HttpResponseMessageProperty.Name, prop);

    return true;
}
}

然后在我声明我的 ServiceHost 对象之后(在打开主机之前)我添加了这一行

host.Authorization.ServiceAuthorizationManager = new MyServiceAuthorizationManager();

执行此操作后,重建并运行我的服务,错误消息停止显示。万岁!

最后,我阅读了一篇文章,其中描述了 ServiceHost 类是为 SOAP/WSDL 服务而不是 RESTful 服务设计的。对于 RESTful 服务,应使用 WebServiceHost 对象。

所以

ServiceHost host = new ServiceHost(new MyService());
host.Open();

变成

WebServiceHost host = new WebServiceHost(new MyService());
host.Open();

您必须添加对以下程序集的引用:

  • System.ServiceModel.Web

希望这可以帮助。

资料来源:

于 2012-10-29T17:58:27.937 回答
1

通常你把它放在响应的标题中。所以把它放在你修改/插入其他标题值的标题中

header('Access-Control-Allow-Origin: *) //change it according to however header is  set in wcf , since this is php syntax

重点是您的响应应该有这个标题。

于 2011-06-10T15:19:42.803 回答
1

您使用 OPTIONS 方法和 Access-Control-Request-Method: DELETE 标头看到的请求称为“预检请求”。CORS 规范要求对具有副作用的方法(如 DELETE)的请求进行此操作,以确保资源可以接受请求。

查看规范的这一部分 >> http://www.w3.org/TR/cors/#cross-origin-request-with-preflight0

不幸的是,我不知道如何使这种类型的请求与 wcf web api 一起工作。

于 2012-01-27T03:43:22.997 回答
0

我使用以下响应标头使其工作:

res.writeHead(200, {
        'Content-Type': 'text/plain',
        'Access-Control-Allow-Methods': 'DELETE, POST, GET, OPTIONS',
        'Access-Control-Allow-Origin': '*'
    }); 
于 2011-12-26T19:07:03.393 回答
0

首先,对于大多数网络浏览器,实际上无法绕过跨域限制。大多数甚至不会让您更改“接受”标题。所以你必须使用 JSONP。JSONP 是一种从跨域服务获取 JSON 数据的方式,但它以 javascript 片段的形式返回 - 这是允许的。它的工作方式是您向服务提供回调函数名称,然后跨域服务返回一个简单的 javascript,其中嵌入了实际 JSON 值作为回调函数的参数。现在使用 WCF WebApi(预览版 6)真的很容易做到这一点。使用 NuGet 在 VS 2010 中安装它。安装后,请在此处查看更多信息。

于 2012-01-25T07:30:12.317 回答
0

我创造了

AllowCrossDomainRequestHandler : DelegatingChannel 

对于每个响应,我都在注册这个标头:

response.Headers.Add("Access-Control-Allow-Origin", "*"); 
于 2011-06-11T17:02:10.333 回答