簡體   English   中英

dio put操作無法正常工作

[英]dio put operation not working in fllutter

我正在嘗試使用休息中心 api 將 flutter web 應用程序與 mongodb 連接起來。 在 mongoDB 中創建新集合需要 PUT 操作。 我檢查了 Postman 中的請求,它工作正常。 我可以從已經存在的集合中獲取數據(GET、POST 工作正常)。

代碼:

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: 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)

我還在 flutter 中嘗試了 http package,得到了類似的錯誤。

這是 PUT 請求201Created的郵遞員狀態

交叉原點出現 XMLHttpRequest 錯誤。 使用此監聽器

  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