GithubHelp home page GithubHelp logo

Comments (8)

AlexV525 avatar AlexV525 commented on June 23, 2024 2

I wish it can help someone who are under same issues and I also wish this can help you, author, to develop this lovable library. (deeply appreciate your efforts) Still wonder why and how but anyway It is solved...

See https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#simple_requests which defines the list of content-type which could be treated as simple requests.

from dio.

AlexV525 avatar AlexV525 commented on June 23, 2024

https://pub.dev/documentation/dio/latest/browser/BrowserHttpClientAdapter/withCredentials.html

from dio.

momadvisor avatar momadvisor commented on June 23, 2024

https://pub.dev/documentation/dio/latest/browser/BrowserHttpClientAdapter/withCredentials.html

Thanks. But Its default value is false. And even when I set it false, It occurs an same error. I think the 'Origin' header should not exist.

dioClient.options.extra['withCredentials'] = false;

This is an error message on the chrome console.
Access to XMLHttpRequest at 'https://api.xxx.com/' from origin 'http://localhost:10545' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

from dio.

AlexV525 avatar AlexV525 commented on June 23, 2024

Then it should not related to Cookies. The request itself might considered CORS.

from dio.

momadvisor avatar momadvisor commented on June 23, 2024

I wonder why It can be resolved just by replacing dio client with http library? Maybe, doesn't http library set the Origin header?(Maybe, in contrast, dio client sets the Origin header)

from dio.

momadvisor avatar momadvisor commented on June 23, 2024

Yea. you're right. After more debugging, I found the problem was not related to cookies, but to header fields. But until now, I couldn't find exactly and specifically which is a problem and how to solve it

from dio.

AlexV525 avatar AlexV525 commented on June 23, 2024

Maybe #2125 (comment)

from dio.

momadvisor avatar momadvisor commented on June 23, 2024

After removing 'contentType' property, It is solved.

dio: 5.4.2+1
flutter: 3.19.2
chrome: 124.0.6367.61 (windows 10)

BEFORE

final client = Dio(
    BaseOptions(
      baseUrl: 'https://api.xxx.com/',
      connectTimeout: const Duration(seconds: 5),
      receiveTimeout: const Duration(seconds: 10),
      headers: {
        HttpHeaders.userAgentHeader: ua,
      },
      contentType: Headers.jsonContentType,
      responseType: ResponseType.json,
    ),
  );

AFTER

final client = Dio(
    BaseOptions(
      baseUrl: 'https://api.xxx.com/',
      connectTimeout: const Duration(seconds: 5),
      receiveTimeout: const Duration(seconds: 10),
      headers: {
        HttpHeaders.userAgentHeader: ua,
      },
      // remove this property
      //contentType: Headers.jsonContentType,
      responseType: ResponseType.json,
    ),
  );

I wish it can help someone who are under same issues and I also wish this can help you, author, to develop this lovable library. (deeply appreciate your efforts)
Still wonder why and how but anyway It is solved...

from dio.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.