javascript 跨域调用、REST 库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11208228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 12:24:07  来源:igfitidea点击:

Cross domain calls, REST library

javascriptapirestcross-domain

提问by Henrik Kjus Alstad

I'm trying to use an external REST Library for my application, and I'm hoping to keep it client side, with javascript. I have no control over the REST library servers, so I cannot modify any access rights or anything on the servers. (It's kept secure by OAuth).

我正在尝试为我的应用程序使用外部 REST 库,我希望使用 javascript 将其保留在客户端。我无法控制 REST 库服务器,因此我无法修改服务器上的任何访问权限或任何内容。(它由 OAuth 保护)。

My question is: Is it possible to connect with the REST API using only javascript? I'm bound to running into cross domain call problems. Using JSONP is not possible from what I gather, since I would only be able to do GET's and not POST's, right? Also, I don't know a lot about JSONP...does it perhaps require me to make some changes on the API-offering server too?(in which case I can't).

我的问题是:是否可以仅使用 javascript 与 REST API 连接?我一定会遇到跨域调用问题。从我收集的信息来看,使用 JSONP 是不可能的,因为我只能执行 GET 而不能执行 POST,对吗?另外,我对 JSONP 了解不多……它是否也需要我对提供 API 的服务器进行一些更改?(在这种情况下我不能)。

Is there a way to workaround this problem, or will I be forced to do some serverside programming?

有没有办法解决这个问题,或者我会被迫做一些服务器端编程吗?

采纳答案by albertjan

There is CORSfor modern browsers. Then there are a couple of other hacks, described very well here: Ways to circumvent the same-origin policy.

现代浏览器有CORS。然后还有一些其他的 hacks,在这里描述得很好:绕过同源策略的方法

Otherwise you'll have to do the server side programming.

否则,您将不得不进行服务器端编程。