跨域访问来自javascript的服务 [英] cross domain access to service from javascript

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

问题描述


我们现有的asp net应用程序也包含web服务。此服务标有ScriptService属性。
我们想要的是允许来自"第三方"的jscript的调用。网页。我尝试在名为WebApp2的新Web应用程序中构建asp.net页面,并从中引用此类服务​​,例如 

< form id =" form1" RUNAT = QUOT;服务器"> 
< asp:ScriptManager runat =" server">
<服务>
< asp:ServiceReference Path =" http://< serverName> /WebApp1/services/TestService.asmx" />
< / Services>来自jscript的
< / asp:ScriptManager>

我需要调用它

< script type =" text / javascript"> 
函数updateAlarm(){
var client = new TestPortal.TestService();
var res = client.TestMethod();
}
< / script>

当jscript运行的服务和页面位于同一个Web应用程序时,此方法有效。但是,情况并非如此,并且不起作用。我需要做些什么才能让它运转?

感谢您的帮助

Lubos一定次数

解决方案

任何形式的纯ajax(XmlHttpRequest)都不支持跨域调用。


但是幸运的是,有一些变通方法可以完成这项工作,这通常通过在您的域上充当代理服务器的页面/处理程序来完成,该代理服务器对此外部域进行Web请求并转发结果。您可以使用 HttpWebRequest和 HttpWebResponse类。

此链接为此提供了一个示例实现。和这里是另一个链接更多细节
如果您不想制作此页面,那么您可以使用纯JavaScript来使用 http:/ /www.ajax-cross-domain.com/
左右:注意一些浏览器(显然只在客户端)可以调整为使用跨域Ajax,检查这个


Hi,

we have existing asp net application containing also web service. This service is marked with ScriptService attribute.
What we want is to allow calls from jscript from "3rd party" web pages. I tried to build-up asp.net page in new web application called WebApp2 and reference such service from it like 

    <form id="form1" runat="server">
    <asp:ScriptManager runat="server">
        <Services >
            <asp:ServiceReference Path="http://<serverName>/WebApp1/services/TestService.asmx"/>
        </Services>
    </asp:ScriptManager>

from jscript i need to call it

    <script type="text/javascript">
        function updateAlarm() {
            var client = new TestPortal.TestService();
            var res = client.TestMethod();
        }    
    </script>

This works when service and page where the jscript runs from are on same web app. However, this is not the case and it doesn't work. What i need to do in order to get it running?

thanks for any help

Lubos



解决方案

Pure ajax (XmlHttpRequest) in any forms does not support cross domain calls.

But fortunately there workarounds to make this work, this generally done by having a page/handler which acts as a proxy on your domain that makes a webrequest to this external domain and forwards the results. You can use the HttpWebRequest and HttpWebResponse classes for this.

This link provides a sample implementation for this.
And here is another link with more details
If you dont want to make this page then you can use pure javascript to make this work using http://www.ajax-cross-domain.com/

PS: On a side note some browsers (obviously only at client side) can be tweaked to use cross-domain Ajax, check this


这篇关于跨域访问来自javascript的服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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