Cross-site ajax call to a WCF Service

前端 未结 4 1455
陌清茗
陌清茗 2021-02-06 18:09

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

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

But I\'ve heard that these days, browsers don\'t

4条回答
  •  孤城傲影
    2021-02-06 18:31

    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.

    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.

    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.

    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.

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

提交回复
热议问题