简体   繁体   English

服务器端Dart中的跨域请求

[英]Cross domain requests in server-side Dart

I write a bunch of scripts in javascript and want to switch to dart or at least start using it. 我用JavaScript写了一堆脚本,想切换到dart或至少开始使用它。 I do have one question though: i know js doesn't support x-domain requests in the browser, but what about running a dart application/script from the server, is it still the same? 我确实有一个问题:我知道js在浏览器中不支持x域请求,但是从服务器运行dart应用程序/脚本又如何呢? can this even be done? 甚至可以做到吗?

basically, since i have no access to the web server to which i query, cross domain ability is a big necessity. 基本上,由于我无权访问要查询的Web服务器,因此跨域功能非常重要。

It sounds like you might be asking about writing a server side command line script which can make requests to an HTTP server. 听起来您可能正在询问编写一个可以向HTTP服务器发出请求的服务器端命令行脚本。 Though the wording of question isn't totally clear to me. 尽管问题的措词对我来说还不是很清楚。 (The answers above are about browser based Dart scripts.) (以上答案与基于浏览器的Dart脚本有关。)

This is possible with Dart. Dart可以做到这一点。 There are no cross origin restrictions in this case. 在这种情况下,没有跨源限制。

See the HttpClient class. 请参见HttpClient类。 Or you can use the http package on pub. 或者,您可以在pub上使用http包。

I recommend using the http package, as it provides a simpler high-level interface. 我建议使用http包,因为它提供了更简单的高级界面。

Here is an example using the http package: 这是使用http包的示例:

import 'dart:io';
import 'package:http/http.dart' as http;

main() {
    http.read("http://google.com").then((content) {
        print(content);
    });
}

You'll need to update your pubspec.yaml file to add the following dependencies: 您需要更新pubspec.yaml文件以添加以下依赖项:

name: Http Example
   dependencies:
     http: any
     pathos: any

(Actually, you should only need to include http, but I think the http package is missing the pathos dependency in it's pubspec.yaml file.) (实际上,您只需要包含http,但是我认为http包的pubspec.yaml文件中缺少对pathos的依赖。)

I couldn't find the pretty documentation for http, but there is some doc comments in the source file . 我找不到http的漂亮文档,但是源文件中有一些文档注释。

Cross domain security is built into the browser, so is neither a feature of Dart or JavaScript. 跨域安全性内置于浏览器中,因此Dart或JavaScript都没有。

Useful for testing: you can pass flags into chrome that will disable this security feature. 对于测试很有用:您可以将标记传递到chrome中,以禁用此安全功能。 See here: http://joshuamcginnis.com/2011/02/28/how-to-disable-same-origin-policy-in-chrome/ 看到这里: http : //joshuamcginnis.com/2011/02/28/how-to-disable-same-origin-policy-in-chrome/

If you want to do a GET request, then you can use Dart JavaScript interop, see this section in this article: http://www.dartlang.org/articles/json-web-service/#note-on-cors 如果要执行GET请求,则可以使用Dart JavaScript互操作,请参阅本文中的本节: http : //www.dartlang.org/articles/json-web-service/#note-on-cors

If you want to POST requests on the other hand, you'll run into problems unless the target server supports CORS. 另一方面,如果您要发布请求,除非目标服务器支持CORS,否则您将遇到问题。 More details here: https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS 此处有更多详细信息: https : //developer.mozilla.org/zh-CN/docs/HTTP/Access_control_CORS

Edit: The correct approach would be to have your client-side code communicate with a server you own, and have that server communicate with the third-party server. 编辑:正确的方法是让您的客户端代码与您拥有的服务器通信,并使该服务器与第三方服务器通信。

You can enable cross-origin requests on the server by setting the Access-Control-Allow-Origin header on the http response. 您可以通过在http响应上设置Access-Control-Allow-Origin标头来在服务器上启用跨域请求。 The value is either * to allow any origin to access the resource, but it is definitely safer to specify valid origins. 值为*允许任何源访问资源,但是指定有效的源绝对更安全。

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

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