服务器端Dart中的跨网域请求 [英] Cross domain requests in server-side Dart

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

问题描述

我在javascript中写了一堆脚本,想要切换到dart或至少开始使用它。我有一个问题,虽然:我知道js不支持浏览器中的x域请求,但是如何从服务器运行dart应用程序/脚本,是否仍然相同?可以这样做?

I write a bunch of scripts in javascript and want to switch to dart or at least start using it. 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? 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.

推荐答案

听起来你可能会问一个服务器端命令行脚本,它可以向HTTP服务器发出请求。虽然我的问题的措辞并不完全清楚。 (上面的答案是关于基于浏览器的Dart脚本。)

It sounds like you might be asking about writing a server side command line script which can make requests to an HTTP server. 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. There are no cross origin restrictions in this case.

请参阅 HttpClient 类。
或者您可以在pub上使用 http 包。

我推荐使用http包,因为它提供了一个更简单的高级接口。

I recommend using the http package, as it provides a simpler high-level interface.

这里是一个使用http包的例子: / p>

Here is an example using the http package:

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

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


$ b $ p

您需要更新pubspec.yaml文件才能添加以下依赖项:

You'll need to update your pubspec.yaml file to add the following dependencies:

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

(实际上,你应该只需要包括http,但我认为http包缺少pathos依赖在它的pubspec.yaml文件。)

(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的漂亮文档,但在源文件

I couldn't find the pretty documentation for http, but there is some doc comments in the source file.

这篇关于服务器端Dart中的跨网域请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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