简体   繁体   English

跨域调用WCF的问题

[英]Issue with calling WCF cross domain

I'm trying to call a WCF service cross-domain using javascript, over http. 我正在尝试使用JavaScript通过HTTP调用WCF服务跨域。

I've added the Access-Control-Allow-Origin header to the operation I want to call like so: 我已经将Access-Control-Allow-Origin标头添加到要调用的操作中,如下所示:

WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*");

However when I post using jquery.ajax I get: 但是,当我使用jquery.ajax发布时,我得到:

Origin http://server/ is not allowed by Access-Control-Allow-Origin.

What's worse is it even happens from my local machine: 更糟糕的是,它甚至发生在我的本地计算机上:

XMLHttpRequest cannot load http://localhost:8201/wcfservice. Origin http://localhost/ is not allowed by Access-Control-Allow-Origin. (observed in Chrome)

am I doing something fundamentally wrong? 我从根本上做错了吗?

What you're doing is fine for GET requests (in CORS-enabled browsers) - the request is sent to the service, and the browser will let it back to the application if the service has the Access-Control-Allow-Origin header). 对于GET请求(在启用了CORS的浏览器中),您正在做的事情很好-请求被发送到服务,并且如果服务具有Access-Control-Allow-Origin标头,浏览器会将其返回给应用程序。

For "unsafe" requests (ie, those which can potentially cause side effects in the service, such as POST, PUT and DELETE), the browser first sends a preflight request, an OPTIONS request which "asks" the service whether it can accept a cross-domain request. 对于“不安全”请求(即那些可能在服务中造成副作用的请求,例如POST,PUT和DELETE),浏览器首先发送一个预检请求,一个OPTIONS请求,该请求“询问”服务是否可以接受服务。跨域请求。 Since your operation only responds to POST requests, it won't respond to that request and the browser will block it. 由于您的操作仅响应POST请求,因此不会响应该请求,浏览器将阻止它。

I wrote a post about one way of implementing CORS support in WCF, you can find it at http://blogs.msdn.com/b/carlosfigueira/archive/2012/05/15/implementing-cors-support-in-wcf.aspx . 我写了一篇关于在WCF中实现CORS支持的方法的帖子,您可以在http://blogs.msdn.com/b/carlosfigueira/archive/2012/05/15/implementing-cors-support-in-wcf中找到它.aspx Basically, you'll need to handle the OPTIONS requests as well, in addition to the "normal" requests. 基本上,除了“正常”请求之外,您还需要处理OPTIONS请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM