简体   繁体   English

WCF JavaScript代理跨域阻止访问数据库

[英]WCF javascript proxy cross domain prevent access database

So I just follow this tutorial to implement a wcf service : http://www.codeproject.com/Articles/627082/How-to-Consume-WCF-Service-using-JavaScript-Proxy . 因此,我只是按照本教程来实现wcf服务: http : //www.codeproject.com/Articles/627082/How-to-Consume-WCF-Service-using-JavaScript-Proxy

In summary: 综上所述:

  1. I wrote a WCF service and make it expose a JS proxy (like in the above tutorial) 我编写了WCF服务,并使其公开了JS代理(如上述教程中所示)
  2. I used WCF test client to test and it works perfectly 我使用WCF测试客户端进行测试,并且效果很好
  3. I wrote a dummy html page and host it on another domain. 我写了一个虚拟的html页面,并将其托管在另一个域上。 Then I cannot access the service due to CORS (Tried to resolve this but no success). 然后由于CORS,我无法访问该服务(试图解决此问题,但未成功)。 So I managed to use chrome with --disable-web-security flag to hack the CORS problem (I will try to solve this later). 因此,我设法使用带有--disable-web-security标志的chrome来破解CORS问题(稍后我将尝试解决此问题)。 Now I can access the service and it works fine but for the functions that have to access the database, they just return empty result. 现在,我可以访问该服务,它可以正常工作,但是对于必须访问数据库的功能,它们只是返回空结果。

So my question is: Is this the normal architecture for this kind of WCF service (Generating js proxy to expose service to javascript clients). 所以我的问题是:这是这种WCF服务的常规体系结构(生成js代理以将服务公开给javascript客户端)。 I write this WCF to expose the service to mobile devices. 我编写此WCF是为了将服务公开给移动设备。 And if yes, is the CORS that preventing me from accessing the database when calling the service from client javascript? 如果是的话,是从客户javascript调用服务时,CORS是否阻止我访问数据库?

Try this way 试试这个

     $.ajax({
         url:'<ip address/site name>/wcfService.svc/method1',
         contentType: "application/json; charset=UTF-8",
         crossDomain: true,
         dataType:'json',
         data:{a:'<parameter value being sent>'}, //pass any parameter that are expected by the remote WCF service method(method1).
         success:function(response) {
             //Process the data(json/ normal data) retrieved from remote service. 
         }
     });

Note:Enable Access-Control-Allow-Origin option on the remote server end for make it CORS compatible. 注意:在远程服务器端上启用Access-Control-Allow-Origin选项,以使其与CORS兼容。 Best way to do this would be to configure this under behavior level itself, instead of service contract or operation contract level.ref:blogs.microsoft.co.il/idof/2011/07/02/ 最好的方法是在行为级别本身而不是service contractoperation contract级别下进行配置。ref:blogs.microsoft.co.il/idof/2011/07/02/

Happy Coding:) 快乐编码:)

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

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