http post json dart 错误:XMLHttpRequest 错误
Posted
技术标签:
【中文标题】http post json dart 错误:XMLHttpRequest 错误【英文标题】:http post json dart Error: XMLHttpRequest error 【发布时间】:2021-06-03 00:29:00 【问题描述】:我想向服务器发送一个 http post 请求(json 格式),并使用 Flutter 和 dart 以 json 格式返回响应。 我有一个关于 C# 的代码,这是可行的
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://mysite");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
string json = "\"email\":\"no@no.ru\"," +
"\"password\":\"mypass\"," +"\"remember\":\"false\"";
streamWriter.Write(json);
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
var result = streamReader.ReadToEnd();
Console.ReadLine(result.ToString());
在 php 上
$data = array(
'email' => 'no@no.ru',
'password' => 'mypass',
'remember' => 'false'
);
$options = array(
'http' => array(
'method' => 'POST',
'content' => json_encode( $data ),
'header'=> "Content-Type: application/json\r\n" .
"Accept: application/json\r\n"
),
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
)
);
$url='https://mysite';
$context = stream_context_create( $options );
$result = file_get_contents( $url, false, $context );
$response = json_decode($result);
var_dump($response);
但在飞镖上它不起作用
var body = "\"email\":\"no@no.ru\"," + "\"password\":\"mypass\"," +"\"remember\":\"false\"";
var url =
Uri.https('mysite', '/mypath');
Map<String,String> headers =
'Content-type' : 'application/json',
'Accept': 'application/json',
;
http.Response response = await http.post(
url,body:body,headers: headers);
print(response.body);
pubpecs.yaml:
http: ^0.13.0
我认为以下错误是由cors引起的,但我无法在服务器端解决它,因为我写了here
Error: XMLHttpRequest error.
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/patch/core_patch.dart 906:28 get current
packages/http/src/browser_client.dart 71:22 <fn>
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/zone.dart 1613:54 runUnary
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 155:18 handleValue
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 707:44 handleValueCallback
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 736:13 _propagateToListeners
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 533:7 [_complete]
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/stream_pipe.dart 61:11 _cancelAndValue
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/stream.dart 1219:7 <fn>
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 324:14 _checkAndCall
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 329:39 dcall
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/html/dart2js/html_dart2js.dart 37307:58 <fn>
at Object.createErrorWithStack (http://localhost:57471/dart_sdk.js:5356:12)
at Object._rethrow (http://localhost:57471/dart_sdk.js:39475:16)
at async._AsyncCallbackEntry.new.callback (http://localhost:57471/dart_sdk.js:39469:13)
at Object._microtaskLoop (http://localhost:57471/dart_sdk.js:39301:13)
at _startMicrotaskLoop (http://localhost:57471/dart_sdk.js:39307:13)
at http://localhost:57471/dart_sdk.js:34814:9
【问题讨论】:
【参考方案1】:您必须在 Json 正文中转义反斜杠,如果这正是您要发送的正文:
var body = "\\"email\":\\"no@no.ru\\"," + "\\"password\\":\\"mypass\\"," +"\\"remember\\":\\"false\\"";
【讨论】:
此代码不起作用。如果我 print(body) 我得到以下字符串: "email":"no@no.ru","password":"mypass","remember":"false" 一开始我没有看到它,但是您正在将它作为字符流写入您的 C# 类的请求中。这是一个 curl 请求吗? 在 C# 和 php 中一切都对我有用,dart 不起作用 你能回答我的问题吗?因为,我相信 Dart http 本身并不支持 curl 请求。 本机没有,但 dart 有一个 curl 库 pub.dev/packages/curl。这是示例,对我不起作用:***.com/questions/63011704/flutter-curl-post-request以上是关于http post json dart 错误:XMLHttpRequest 错误的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Flutter/Dart 中使用 url 编码的标头和正文发出 HTTP POST 请求
dart - 如何使用 http.post 实时获取我的数据?
Angular2 HTTP POST发生错误SyntaxError:JSON输入意外结束
Flutter/Dart http POST 重定向通过 GET 发送正文
FlutterHTTP 网络操作 ( 引入 http 插件 | 测试网站 | Get 请求 | Post 请求 | 将响应结果转为 Dart 对象 | Future 异步调用 )