简体   繁体   English

dio put操作无法正常工作

[英]dio put operation not working in fllutter

Im trying to connect flutter web app with mongodb using restheart api.我正在尝试使用休息中心 api 将 flutter web 应用程序与 mongodb 连接起来。 PUT operation is required for creating new collection in mongoDB.在 mongoDB 中创建新集合需要 PUT 操作。 I checked request in Postman, its works fine.我检查了 Postman 中的请求,它工作正常。 I can get data from already existing collection (GET, POST works fine).我可以从已经存在的集合中获取数据(GET、POST 工作正常)。

Code:代码:

Dio dio = new Dio();
  dio.interceptors.add(InterceptorsWrapper(onRequest:
   (RequestOptions options) async {
    var customHeaders = {
      'content-type': 'application/json',
      HttpHeaders.authorizationHeader: "Basic $encoded"
    };
    options.headers.addAll(customHeaders);
    return options;      
  }));  
  String url = "http://localhost:8080/toofo";
  response2 = await dio.put(url);
  print("############## ${response2.statusCode} ##########");
  print("############## ${response2.data} ##########");

Error:错误:

 Error: DioError [DioErrorType.RESPONSE]: XMLHttpRequest error.
    at Object.throw_ [as throw] (http://localhost:52789/dart_sdk.js:4773:11)
    at dio_for_browser.DioForBrowser.new.<anonymous> (http://localhost:52789/packages/dio/src/entry/dio_for_browser.dart.lib.js:359:27)
    at Generator.next (<anonymous>)
    at onValue (http://localhost:52789/dart_sdk.js:35655:33)
    at _RootZone.runUnary (http://localhost:52789/dart_sdk.js:35540:58)
    at _FutureListener.thenAwait.handleValue (http://localhost:52789/dart_sdk.js:30956:29)
    at handleValueCallback (http://localhost:52789/dart_sdk.js:31466:49)
    at Function._propagateToListeners (http://localhost:52789/dart_sdk.js:31498:17)
    at _Future.new.[_completeWithValue] (http://localhost:52789/dart_sdk.js:31357:23)
    at async._AsyncCallbackEntry.new.callback (http://localhost:52789/dart_sdk.js:31377:35)
    at Object._microtaskLoop (http://localhost:52789/dart_sdk.js:35756:13)
    at _startMicrotaskLoop (http://localhost:52789/dart_sdk.js:35762:13)

I also tried http package in flutter, got similar error.我还在 flutter 中尝试了 http package,得到了类似的错误。

This is the postman's status of PUT request 201Created这是 PUT 请求201Created的邮递员状态

XMLHttpRequest error occurs for cross origin.交叉原点出现 XMLHttpRequest 错误。 Use this hearder使用此监听器

  headers: {
  'content-type': 'application/json',
 "Access-Control-Allow-Origin": "*", // Required for CORS support to work
 "Access-Control-Allow-Credentials": true, // Required for cookies, authorization   headers with HTTPS
   "Access-Control-Allow-Headers": "Origin,Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token,locale",
  "Access-Control-Allow-Methods": "POST, OPTIONS"
 },

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

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