跨站点ajax调用WCF服务 [英] Cross-site ajax call to a WCF Service

查看:86
本文介绍了跨站点ajax调用WCF服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在Javascript中对WCF服务进行跨站点调用?

Is it possible to do a cross-site call, in Javascript, to a WCF service?

我不介意它是POST还是GET。

I don't mind if it's a POST or a GET.

但是我听说这些天浏览器不允许使用POST或GET进行跨站点调用。

But I've heard that these days, browsers don't allow cross-site calls with either POST or GET.

我如何绕过这个并仍然调用 WCF服务

How can I circumvent this and still call a WCF Service?

推荐答案

没有你可以做很多事情来规避浏览器的跨站点脚本拦截器。这些阻止程序阻止XMLHTTPRequest发生到任何域,但加载包含脚本或页面的域。

There's not a whole lot you can do to circumvent the browser's cross-site scripting blockers. Those blockers stop XMLHTTPRequest's from happening to any domain but the one that loaded the containing script or page.

也就是说,有一种常用的解决方法:使用JavaScript在DOM中编写一个新条目,引用一个跨站点URL的src。您将所有RPC方法参数传递给此脚本,该脚本将返回将要执行的一些JavaScript,告诉您成功或失败。

That said, there is one commonly used workaround: Use JavaScript to write a new entry into the DOM that references a src that is a cross-site URL. You'll pass all your RPC method arguments to this "script" which will return some JavaScript that will be executed, telling you success or failure.

以这种方式无法执行POST,src URL必须是GET,因此您可以通过这种方式传递参数。我不确定WCF是否有仅限GET访问方法。并且,由于浏览器希望远程标记的结果是有效的JavaScript对象,因此您必须确保您的WCF服务也遵守该标准,否则您将收到JavaScript错误。

There's no way to do a POST in this manner, the src URL must be a GET, so you can pass arguments that way. I'm not sure if WCF has a "GET only" method of access. And, since the browser will expect the result of the remote tag to be a valid JavaScript object, you'll have to make sure that your WCF service obeys that as well, otherwise you'll get JavaScript errors.

绕过跨站点脚本的另一种常用方法是为您的请求编写代理。换句话说,如果要从example.com上托管的脚本访问域test.com,请在example.com上创建一些URL,以适当的方式将请求代理到test.com。

Another common method of circumventing cross-site scripting is to write a proxy for your requests. In other words, if you want to access domain test.com from scripts hosted on example.com, then make some URL on example.com that proxies the request over to test.com in the proper way.

对于您的示例,代理可能是正确的答案,假设WCF没有自己的跨站点脚本限制。

For your example, the proxying is likely the right answer, assuming that WCF doesn't have it's own cross-site scripting restrictions.

这篇关于跨站点ajax调用WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆